x86: MCE: Cleanups series
cover x86: MCE: Cleanups series
This series adds a few selected cleanups for the x86 machine check code. It's patches out of the previously submitted mce tree.
They have been all reviewed previously.
The biggest change is the rewritten MCE poller. This cleans up the code significantly and also slightly improves the behavour.
These cleanups are needed for followon patches.
This is submitted for 2.6.30. It was developed against 2.6.29-rc4, but also applies to x86 tip as of today.
It applies on top of the earlier bug fix series.
-Andi
PATCH 1/4 x86: MCE: Enable machine checks in 64bit defconfig
Impact: Low priority fix
The 32bit defconfig already had it enabled. And it's a pretty fundamental feature, so better enable it on 64bit too.
--- arch/x86/configs/x86_64_defconfig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/configs/x86_64_defconfig +4 -1
@@ -247,7 +247,10 @@ # CONFIG_PREEMPT is not set CONFIG_X86_LOCAL_APIC=y CONFIG_X86_IO_APIC=y -# CONFIG_X86_MCE is not set +CONFIG_X86_MCE=y +CONFIG_X86_NEW_MCE=y +CONFIG_X86_MCE_INTEL=y +CONFIG_X86_MCE_AMD=y # CONFIG_I8K is not set CONFIG_MICROCODE=y CONFIG_MICROCODE_OLD_INTERFACE=y
PATCH 2/4 x86: MCE: Implement dynamic machine check banks support v5
In reply to Andi KleenImpact: cleanup; making code future proof; memory saving on small systems
This patch replaces the hardcoded max number of machine check banks with dynamic allocation depending on what the CPU reports. The sysfs data structures and the banks array are dynamically allocated.
There is still a hard bank limit (128) because the mcelog protocol uses banks >= 128 as pseudo banks to escape other events. But we expect that 128 banks is beyond any reasonable CPU for now.
This supersedes an earlier patch by Venki, but it solves the problem more completely by making the limit fully dynamic (upto the 128 boundary)
This saves some memory on machines with less than 6 banks because they won't need sysdevs for unused ones and also allows to use sysfs to control these banks on possible future CPUs with more than 6 banks.
v2: Fix typo in initialization
v3: Fold fix banks message fix into this one.
v4: Fix cap init ordering
v5: Forward port to new patch order
--- arch/x86/kernel/cpu/mcheck/mce_64.c | 139 +++++++++++++++++++++++++++--------- 1 file changed, 107 insertions(+), 32 deletions(-)
arch/x86/kernel/cpu/mcheck/mce_64.c +107 -32
@@ -24,6 +24,8 @@#include <linux/ctype.h> #include <linux/kmod.h> #include <linux/kdebug.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> #include <asm/processor.h> #include <asm/msr.h> #include <asm/mce.h>
@@ -32,7 +34,12 @@#include <asm/idle.h> #define MISC_MCELOG_MINOR 227 -#define NR_SYSFS_BANKS 6 + +/* +* To support more than 128 would need to escape the predefined +* Linux defined extended banks first. +*/ +#define MAX_NR_BANKS (MCE_EXTENDED_BANK - 1)atomic_t mce_entry;
@@ -47,7 +54,7 @@*/ static int tolerant =1 ;static int banks; -static unsigned long bank[NR_SYSFS_BANKS ] = { [0 ...NR_SYSFS_BANKS -1 ] = ~0UL }; +static u64 *bank;static unsigned long notify_user;static int rip_msr;static int mce_bootlog =-1 ;
@@ -212,7 +219,7 @@barrier ();for (i =0 ; i < banks; i++) { -if (i <NR_SYSFS_BANKS && !bank[i]) +if (!bank[i])continue ; m.misc =0 ;
@@ -446,21 +453,36 @@/* * Initialize Machine Checks for a CPU. */ -static void mce_init (void *dummy) +static void mce_cap_init (void ) {u64 cap; -int i;rdmsrl (MSR_IA32_MCG_CAP , cap); - banks = cap &0xff ; -if (banks >MCE_EXTENDED_BANK ) { - banks =MCE_EXTENDED_BANK ; -printk (KERN_INFO "MCE: warning: using only %d banks\n" , -MCE_EXTENDED_BANK ); +/* Handle the unlikely case of one CPU having less banks than others */ +if (banks ==0 || banks > (cap &0xff )) + banks = cap &0xff ; +if (banks >MAX_NR_BANKS ) { + banks =MAX_NR_BANKS ; +printk (KERN_WARNING +"MCE: Using only %d machine check banks out of %u\n" , + banks, (u32 )cap &0xff ); + } +if (!bank) { + bank =kmalloc (banks *sizeof (u64),GFP_KERNEL ); +if (!bank) +return ; +memset (bank,0xff , banks *sizeof (u64)); } +/* Use accurate RIP reporting if available. */ if ((cap & (1 <<9 )) && ((cap >>16 ) &0xff ) >=9 ) rip_msr =MSR_IA32_MCG_EIP ; +} + +static void mce_init (void *dummy) +{ +u64 cap; +int i;/* Log the machine checks left over from the previous reset. This also clears all registers */
@@ -468,15 +490,12 @@set_in_cr4 (X86_CR4_MCE ); +rdmsrl (MSR_IA32_MCG_CAP , cap);if (cap &MCG_CTL_P )wrmsr (MSR_IA32_MCG_CTL ,0xffffffff ,0xffffffff );for (i =0 ; i < banks; i++) { -if (i <NR_SYSFS_BANKS ) -wrmsrl (MSR_IA32_MC0_CTL +4 *i, bank[i]); -else -wrmsrl (MSR_IA32_MC0_CTL +4 *i, ~0UL ); - +wrmsrl (MSR_IA32_MC0_CTL +4 *i, bank[i]);wrmsrl (MSR_IA32_MC0_STATUS +4 *i,0 ); } }
@@ -486,10 +505,10 @@ {/* This should be disabled by the BIOS, but isn't always */ if (c->x86_vendor ==X86_VENDOR_AMD ) { -if (c->x86 ==15 ) +if (c->x86 ==15 && banks >4 )/* disable GART TBL walk error reporting, which trips off incorrectly with the IOMMU & 3ware & Cerberus. */ -clear_bit (10 , &bank[4 ]); +clear_bit (10 , (unsigned long *)&bank[4 ]);if (c->x86 <=17 && mce_bootlog <0 )/* Lots of broken BIOS around that don't clear them by default and leave crap in there. Don't log. */
@@ -532,11 +551,12 @@*/ void __cpuinit mcheck_init (struct cpuinfo_x86 *c) { -mce_cpu_quirks (c); -if (!mce_available (c))return ; +mce_cap_init (); +mce_cpu_quirks (c); +mce_init (NULL );mce_cpu_features (c);mce_init_timer ();
@@ -819,16 +839,26 @@ } \ static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name); -/* -* TBD should generate these dynamically based on number of available banks. -* Have only 6 contol banks in /sysfs until then. -*/ -ACCESSOR(bank0ctl,bank[0],mce_restart()) -ACCESSOR(bank1ctl,bank[1],mce_restart()) -ACCESSOR(bank2ctl,bank[2],mce_restart()) -ACCESSOR(bank3ctl,bank[3],mce_restart()) -ACCESSOR(bank4ctl,bank[4],mce_restart()) -ACCESSOR(bank5ctl,bank[5],mce_restart()) +static struct sysdev_attribute *bank_attrs; + +static ssize_t show_bank (struct sys_device *s,struct sysdev_attribute *attr, +char *buf) +{ +u64 b = bank[attr - bank_attrs]; +return sprintf (buf,"%Lx\n" , b); +} + +static ssize_t set_bank (struct sys_device *s,struct sysdev_attribute *attr, +const char *buf,size_t siz) +{ +char *end; +u64 new =simple_strtoull (buf, &end,0 ); +if (end == buf) +return -EINVAL ; + bank[attr - bank_attrs] = new; +mce_restart (); +return end-buf; +}static ssize_t show_trigger (struct sys_device *s,struct sysdev_attribute *attr,char *buf)
@@ -855,8 +885,6 @@ static SYSDEV_INT_ATTR(tolerant, 0644, tolerant); ACCESSOR(check_interval,check_interval,mce_restart()) static struct sysdev_attribute *mce_attributes[] = { - &attr_bank0ctl, &attr_bank1ctl, &attr_bank2ctl, - &attr_bank3ctl, &attr_bank4ctl, &attr_bank5ctl, &attr_tolerant.attr, &attr_check_interval, &attr_trigger,NULL };
@@ -886,11 +914,22 @@if (err) goto error; } +for (i =0 ; i < banks; i++) { + err =sysdev_create_file (&per_cpu (device_mce, cpu), + &bank_attrs[i]); +if (err) + goto error2; + }cpu_set (cpu, mce_device_initialized);return 0 ; +error2: +while (--i >=0 ) { +sysdev_remove_file (&per_cpu (device_mce, cpu), + &bank_attrs[i]); + } error: -while (i--) { +while (--i >=0 ) {sysdev_remove_file (&per_cpu (device_mce,cpu), mce_attributes[i]); }
@@ -909,6 +948,9 @@for (i =0 ; mce_attributes[i]; i++)sysdev_remove_file (&per_cpu (device_mce,cpu), mce_attributes[i]); +for (i =0 ; i < banks; i++) +sysdev_remove_file (&per_cpu (device_mce, cpu), + &bank_attrs[i]);sysdev_unregister (&per_cpu (device_mce,cpu));cpu_clear (cpu, mce_device_initialized); }
@@ -973,6 +1015,34 @@ .notifier_call = mce_cpu_callback, }; +static __init int mce_init_banks (void ) +{ +int i; + + bank_attrs =kzalloc (sizeof (struct sysdev_attribute ) * banks, +GFP_KERNEL ); +if (!bank_attrs) +return -ENOMEM ; + +for (i =0 ; i < banks; i++) { +struct sysdev_attribute *a = &bank_attrs[i]; + a->attr.name =kasprintf (GFP_KERNEL ,"bank%d" , i); +if (!a->attr.name) + goto nomem; + a->attr.mode =0644 ; + a->show = show_bank; + a->store = set_bank; + } +return 0 ; + +nomem: +while (--i >=0 ) +kfree (bank_attrs[i].attr.name); +kfree (bank_attrs); + bank_attrs =NULL ; +return -ENOMEM ; +} +static __init int mce_init_device (void ) {int err;
@@ -980,6 +1050,11 @@if (!mce_available (&boot_cpu_data))return -EIO ; + + err =mce_init_banks (); +if (err) +return err; + err =sysdev_class_register (&mce_sysclass);if (err)return err;
PATCH 3/4 x86: MCE: Factor out duplicated struct mce setup code into a single function
In reply to Andi KleenImpact: cleanup
This merely factors out duplicated code to set up the initial struct mce state into a single function.
--- arch/x86/include/asm/mce.h | 3 ++- arch/x86/kernel/cpu/mcheck/mce_64.c | 23 ++++++++++++++--------- arch/x86/kernel/cpu/mcheck/mce_amd_64.c | 4 +--- arch/x86/kernel/cpu/mcheck/mce_intel_64.c | 2 +- 4 files changed, 18 insertions(+), 14 deletions(-)
arch/x86/kernel/cpu/mcheck/mce_64.c +14 -9
@@ -65,6 +65,14 @@static DECLARE_WAIT_QUEUE_HEAD (mce_wait); +/* Do initial initialization of a struct mce */ +void mce_setup (struct mce *m) +{ +memset (m,0 ,sizeof (struct mce )); + m->cpu =smp_processor_id (); +rdtscll (m->tsc); +} +/* * Lockless MCE logging infrastructure. * This avoids deadlocks on printk locks without having to break locks. Also
@@ -208,8 +216,8 @@ || !banks) goto out2; -memset (&m,0 ,sizeof (struct mce )); - m.cpu =smp_processor_id (); +mce_setup (&m); +rdmsrl (MSR_IA32_MCG_STATUS , m.mcgstatus);/* if the restart IP is not valid, we're done for */ if (!(m.mcgstatus &MCG_STATUS_RIPV ))
@@ -225,7 +233,6 @@ m.misc =0 ; m.addr =0 ; m.bank = i; - m.tsc =0 ;rdmsrl (MSR_IA32_MC0_STATUS + i*4 , m.status);if ((m.status &MCI_STATUS_VAL ) ==0 )
@@ -252,8 +259,8 @@rdmsrl (MSR_IA32_MC0_ADDR + i*4 , m.addr);mce_get_rip (&m, regs); -if (error_code >=0 ) -rdtscll (m.tsc); +if (error_code <0 ) + m.tsc =0 ;if (error_code !=-2 )mce_log (&m);
@@ -341,15 +348,13 @@* and historically has been the register value of the * MSR_IA32_THERMAL_STATUS (Intel) msr. */ -void mce_log_therm_throt_event (unsigned int cpu,__u64 status) +void mce_log_therm_throt_event (__u64 status) {struct mce m; -memset (&m,0 ,sizeof (m)); - m.cpu = cpu; +mce_setup (&m); m.bank =MCE_THERMAL_BANK ; m.status = status; -rdtscll (m.tsc);mce_log (&m); }#endif /* CONFIG_X86_MCE_INTEL */
arch/x86/include/asm/mce.h +2 -1
@@ -90,6 +90,7 @@#include <asm/atomic.h> +void mce_setup (struct mce *m);void mce_log (struct mce *m); DECLARE_PER_CPU(struct sys_device, device_mce);extern void (*threshold_cpu_callback)(unsigned long action,unsigned int cpu);
@@ -106,7 +107,7 @@static inline void mce_amd_feature_init (struct cpuinfo_x86 *c) { }#endif -void mce_log_therm_throt_event (unsigned int cpu,__u64 status); +void mce_log_therm_throt_event (__u64 status);extern atomic_t mce_entry;
arch/x86/kernel/cpu/mcheck/mce_intel_64.c +1 -1
@@ -24,7 +24,7 @@rdmsrl (MSR_IA32_THERM_STATUS , msr_val);if (therm_throt_process (msr_val &1 )) -mce_log_therm_throt_event (smp_processor_id (), msr_val); +mce_log_therm_throt_event (msr_val);inc_irq_stat (irq_thermal_count);irq_exit ();
arch/x86/kernel/cpu/mcheck/mce_amd_64.c +1 -3
@@ -197,9 +197,7 @@exit_idle ();irq_enter (); -memset (&m,0 ,sizeof (m)); -rdtscll (m.tsc); - m.cpu =smp_processor_id (); +mce_setup (&m);/* assume first bank caused it */ for (bank =0 ; bank <NR_BANKS ; ++bank) {
PATCH 4/4 x86: MCE: Separate correct machine check poller and fatal exception handler v2
In reply to Andi KleenImpact: cleanup
The machine check poller is diverging more and more from the fatal exception handler. Instead of adding more special cases separate the code paths completely. The corrected poll path is actually quite simple, and this doesn't result in much code duplication.
This makes both handlers much easier to read and results in cleaner code flow. The exception handler now only needs to care about uncorrected errors, which also simplifies the handling of multiple errors. The corrected poller also now always runs in standard interrupt context and does not need to do anything special to handle NMI context.
Minor behaviour changes:
- MCG status is now not cleared on polling.
- Only the banks which had corrected errors get cleared on polling
- The exception handler only clears banks with errors now
v2: Forward port to new patch order. Add "uc" argument.
--- arch/x86/include/asm/mce.h | 7 + arch/x86/kernel/cpu/mcheck/mce_64.c | 129 ++++++++++++++++++++++++++------ arch/x86/kernel/cpu/mcheck/mce_amd_64.c | 2 3 files changed, 116 insertions(+), 22 deletions(-)
arch/x86/kernel/cpu/mcheck/mce_64.c +108 -21
@@ -3,6 +3,8 @@* K8 parts Copyright 2002,2003 Andi Kleen, SuSE Labs. * Rest from unknown author(s). * 2004 Andi Kleen. Rewrote most of it. +* Copyright 2008 Intel Corporation +* Author: Andi Kleen */ #include <linux/init.h>
@@ -189,7 +191,77 @@ }/* -* The actual machine check handler +* Poll for corrected events or events that happened before reset. +* Those are just logged through /dev/mcelog. +* +* This is executed in standard interrupt context. +*/ +void machine_check_poll (enum mcp_flags flags) +{ +struct mce m; +int i; + +mce_setup (&m); + +rdmsrl (MSR_IA32_MCG_STATUS , m.mcgstatus); +for (i =0 ; i < banks; i++) { +if (!bank[i]) +continue ; + + m.misc =0 ; + m.addr =0 ; + m.bank = i; + m.tsc =0 ; + +barrier (); +rdmsrl (MSR_IA32_MC0_STATUS + i*4 , m.status); +if (!(m.status &MCI_STATUS_VAL )) +continue ; + +/* +* Uncorrected events are handled by the exception handler +* when it is enabled. But when the exception is disabled log +* everything. +* +* TBD do the same check for MCI_STATUS_EN here? +*/ +if ((m.status &MCI_STATUS_UC ) && !(flags &MCP_UC )) +continue ; + +if (m.status &MCI_STATUS_MISCV ) +rdmsrl (MSR_IA32_MC0_MISC + i*4 , m.misc); +if (m.status &MCI_STATUS_ADDRV ) +rdmsrl (MSR_IA32_MC0_ADDR + i*4 , m.addr); + +if (!(flags &MCP_TIMESTAMP )) + m.tsc =0 ; +/* +* Don't get the IP here because it's unlikely to +* have anything to do with the actual error location. +*/ + +mce_log (&m); +add_taint (TAINT_MACHINE_CHECK ); + +/* +* Clear state for this bank. +*/ +wrmsrl (MSR_IA32_MC0_STATUS +4 *i,0 ); + } + +/* +* Don't clear MCG_STATUS here because it's only defined for +* exceptions. +*/ +} + +/* +* The actual machine check handler. This only handles real +* exceptions when something got corrupted coming in through int 18. +* +* This is executed in NMI context not subject to normal locking rules. This +* implies that most kernel services cannot be safely used. Don't even +* think about putting a printk in there! */ void do_machine_check (struct pt_regs * regs,long error_code) {
@@ -207,13 +279,14 @@* error. */ int kill_it =0 ; +DECLARE_BITMAP (toclear,MAX_NR_BANKS );atomic_inc (&mce_entry); -if ((regs - &¬ify_die (DIE_NMI ,"machine check" , regs, error_code, +if (notify_die (DIE_NMI ,"machine check" , regs, error_code,18 ,SIGKILL ) ==NOTIFY_STOP ) - || !banks) + goto out2; +if (!banks) goto out2;mce_setup (&m);
@@ -227,6 +300,7 @@barrier ();for (i =0 ; i < banks; i++) { +__clear_bit (i, toclear);if (!bank[i])continue ;
@@ -238,6 +312,20 @@if ((m.status &MCI_STATUS_VAL ) ==0 )continue ; +/* +* Non uncorrected errors are handled by machine_check_poll +* Leave them alone. +*/ +if ((m.status &MCI_STATUS_UC ) ==0 ) +continue ; + +/* +* Set taint even when machine check was not enabled. +*/ +add_taint (TAINT_MACHINE_CHECK ); + +__set_bit (i, toclear); +if (m.status &MCI_STATUS_EN ) {/* if PCC was set, there's no way out */ no_way_out |= !!(m.status &MCI_STATUS_PCC );
@@ -251,6 +339,12 @@ no_way_out =1 ; kill_it =1 ; } + }else { +/* +* Machine check event was not enabled. Clear, but +* ignore. +*/ +continue ; }if (m.status &MCI_STATUS_MISCV )
@@ -259,10 +353,7 @@rdmsrl (MSR_IA32_MC0_ADDR + i*4 , m.addr);mce_get_rip (&m, regs); -if (error_code <0 ) - m.tsc =0 ; -if (error_code !=-2 ) -mce_log (&m); +mce_log (&m);/* Did this bank cause the exception? */ /* Assume that the bank with uncorrectable errors did it,
@@ -271,14 +362,8 @@ panicm = m; panicm_found =1 ; } - -add_taint (TAINT_MACHINE_CHECK ); } -/* Never do anything final in the polling timer */ -if (!regs) - goto out; -/* If we didn't find an uncorrectable error, pick the last one (shouldn't happen, just being safe). */ if (!panicm_found)
@@ -325,10 +410,11 @@/* notify userspace ASAP */ set_thread_flag (TIF_MCE_NOTIFY ); - out:/* the last thing we do is clear state */ -for (i =0 ; i < banks; i++) -wrmsrl (MSR_IA32_MC0_STATUS +4 *i,0 ); +for (i =0 ; i < banks; i++) { +if (test_bit (i, toclear)) +wrmsrl (MSR_IA32_MC0_STATUS +4 *i,0 ); + }wrmsrl (MSR_IA32_MCG_STATUS ,0 ); out2:atomic_dec (&mce_entry);
@@ -377,7 +463,7 @@WARN_ON (smp_processor_id () != data);if (mce_available (¤t_cpu_data)) -do_machine_check (NULL ,0 ); +machine_check_poll (MCP_TIMESTAMP );/* * Alert userspace if needed. If we logged an MCE, reduce the
@@ -489,9 +575,10 @@u64 cap;int i; -/* Log the machine checks left over from the previous reset. -This also clears all registers */ -do_machine_check (NULL , mce_bootlog ?-1 :-2 ); +/* +* Log the machine checks left over from the previous reset. +*/ +machine_check_poll (MCP_UC );set_in_cr4 (X86_CR4_MCE );
arch/x86/kernel/cpu/mcheck/mce_amd_64.c +1 -1
@@ -231,7 +231,7 @@/* Log the machine check that caused the threshold event. */ -do_machine_check (NULL ,0 ); +machine_check_poll (MCP_TIMESTAMP );if (high &MASK_OVERFLOW_HI ) {rdmsrl (address, m.misc);
arch/x86/include/asm/mce.h +7 -0
@@ -112,6 +112,13 @@extern atomic_t mce_entry;extern void do_machine_check (struct pt_regs *,long ); + +enum mcp_flags { +MCP_TIMESTAMP = (1 <<0 ),/* log time stamp */ +MCP_UC = (1 <<1 ),/* log uncorrected errors */ +}; +extern void machine_check_poll (enum mcp_flags flags); +extern int mce_notify_user (void );#endif /* !CONFIG_X86_32 */
Re: [PATCH] [2/4] x86: MCE: Implement dynamic machine check banks support v5
In reply to Andi KleenQuoted 85 lines · Andi Kleen
Impact: cleanup; making code future proof; memory saving on small systems
This patch replaces the hardcoded max number of machine check banks with dynamic allocation depending on what the CPU reports. The sysfs data structures and the banks array are dynamically allocated.
There is still a hard bank limit (128) because the mcelog protocol uses banks >= 128 as pseudo banks to escape other events. But we expect that 128 banks is beyond any reasonable CPU for now.
This supersedes an earlier patch by Venki, but it solves the problem more completely by making the limit fully dynamic (upto the 128 boundary)
This saves some memory on machines with less than 6 banks because they won't need sysdevs for unused ones and also allows to use sysfs to control these banks on possible future CPUs with more than 6 banks.
v2: Fix typo in initialization
v3: Fold fix banks message fix into this one.
v4: Fix cap init ordering
v5: Forward port to new patch orderCc Venki Pallipadi <[email protected]>Signed-off-by Andi Kleen <[email protected]>--- arch/x86/kernel/cpu/mcheck/mce_64.c | 139 +++++++++++++++++++++++++++--------- 1 file changed, 107 insertions(+), 32 deletions(-)Index:
linux/arch/x86/kernel/cpu/mcheck/mce_64.c
===================================================================arch/x86/kernel/cpu/mcheck/mce_64.c +11
-5@@ -24,6 +24,8 @@#include <linux/ctype.h> #include <linux/kmod.h> #include <linux/kdebug.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> #include <asm/processor.h> #include <asm/msr.h> #include <asm/mce.h> @@ -32,7 +34,12 @@#include <asm/idle.h> #define MISC_MCELOG_MINOR 227 -#define NR_SYSFS_BANKS 6 + +/* +* To support more than 128 would need to escape the predefined +* Linux defined extended banks first. +*/ +#define MAX_NR_BANKS (MCE_EXTENDED_BANK - 1)atomic_t mce_entry;@@ -47,7 +54,7 @@*/ static int tolerant =1 ;static int banks; -static unsigned long bank[NR_SYSFS_BANKS ] = { [0 ...NR_SYSFS_BANKS -1 ] = ~0UL }; +static u64 *bank;static unsigned long notify_user;static int rip_msr;static int mce_bootlog =-1 ;@@ -212,7 +219,7 @@barrier ();for (i =0 ; i < banks; i++) { -if (i <NR_SYSFS_BANKS && !bank[i]) +if (!bank[i])continue ; m.misc =0 ;@@ -446,21 +453,36 @@/* * Initialize Machine Checks for a CPU. */ -static void mce_init (void *dummy) +static void mce_cap_init (void ) {u64 cap; -int i;
arch/x86/kernel/cpu/mcheck/mce_64.c line 463
Quoted 10 lines
arch/x86/kernel/cpu/mcheck/mce_64.c +3
-5rdmsrl (MSR_IA32_MCG_CAP , cap); - banks = cap &0xff ; -if (banks >MCE_EXTENDED_BANK ) { - banks =MCE_EXTENDED_BANK ; -printk (KERN_INFO "MCE: warning: using only %d banks\n" , -MCE_EXTENDED_BANK ); +/* Handle the unlikely case of one CPU having less banks than others */ +if (banks ==0 || banks > (cap &0xff )) + banks = cap &0xff ;
Do we need a per cpu count of # of banks to handle one CPU having less banks than others case?
Specifically, I am thinking of sequence:
- CPU 0 has n banks
- CPU 0 does below
for (i = 0; i < banks; i++) {
err = sysdev_create_file(&per_cpu(device_mce, cpu),
&bank_attrs[i]);- CPU 1, which comes online later, has say n-2 banks. So, banks now becomes n-2.
- Now whenever CPU 0 does sysdev_remove_file loop below, it will do it only for n-2 banks.
Thanks,
Venki
Re: [PATCH] [2/4] x86: MCE: Implement dynamic machine check banks support v5
Quoted 10 lines
Do we need a per cpu count of # of banks to handle one CPU having less banks than others case?
Specifically, I am thinking of sequence:
- CPU 0 has n banks
- CPU 0 does belowfor (i = 0; i < banks; i++) { err = sysdev_create_file(&per_cpu(device_mce, cpu), &bank_attrs[i]);- CPU 1, which comes online later, has say n-2 banks. So, banks now becomes n-2.
- Now whenever CPU 0 doessysdev_remove_fileloop below, it will do it only for n-2 banks.
True, that would be a leak without per cpu banks count. Perhaps should take it out, this is really not supposed to happen anyways. I'm sure more things would break with an asymetric CPU configuration.
An alternative would be to figure out how to walk the sysfs data structures for this, but that would be really too much effort for something that shouldn't happen.
I think I'll just add a WARN_ON() for this case instead.
-Andi
PATCH 1/1 x86: MCE: Implement dynamic machine check banks support v6
In reply to Pallipadi, VenkateshHere's the updated patch addressing Venki's comments. I also added in another patch from Thomas which fixed the error allocation path (that patch was previously separated)
---
x86: MCE: Implement dynamic machine check banks support v6
Impact: cleanup; making code future proof; memory saving on small systems
This patch replaces the hardcoded max number of machine check banks with dynamic allocation depending on what the CPU reports. The sysfs data structures and the banks array are dynamically allocated.
There is still a hard bank limit (128) because the mcelog protocol uses banks >= 128 as pseudo banks to escape other events. But we expect that 128 banks is beyond any reasonable CPU for now.
This supersedes an earlier patch by Venki, but it solves the problem more completely by making the limit fully dynamic (upto the 128 boundary)
This saves some memory on machines with less than 6 banks because they won't need sysdevs for unused ones and also allows to use sysfs to control these banks on possible future CPUs with more than 6 banks.
v2: Fix typo in initialization
v3: Fold fix banks message fix into this one.
v4: Fix cap init ordering
v5: Forward port to new patch order
v6: Use WARN_ON to complain about asymmetric configurations
(based on comments from Venki)
Integrate Thomas Gleixner's memory allocation handling fix
--- arch/x86/kernel/cpu/mcheck/mce_64.c | 147 ++++++++++++++++++++++++++++-------- 1 file changed, 115 insertions(+), 32 deletions(-)
arch/x86/kernel/cpu/mcheck/mce_64.c +115 -32
@@ -24,6 +24,8 @@#include <linux/ctype.h> #include <linux/kmod.h> #include <linux/kdebug.h> +#include <linux/kobject.h> +#include <linux/sysfs.h> #include <asm/processor.h> #include <asm/msr.h> #include <asm/mce.h>
@@ -32,7 +34,12 @@#include <asm/idle.h> #define MISC_MCELOG_MINOR 227 -#define NR_SYSFS_BANKS 6 + +/* +* To support more than 128 would need to escape the predefined +* Linux defined extended banks first. +*/ +#define MAX_NR_BANKS (MCE_EXTENDED_BANK - 1)atomic_t mce_entry;
@@ -47,7 +54,7 @@*/ static int tolerant =1 ;static int banks; -static unsigned long bank[NR_SYSFS_BANKS ] = { [0 ...NR_SYSFS_BANKS -1 ] = ~0UL }; +static u64 *bank;static unsigned long notify_user;static int rip_msr;static int mce_bootlog =-1 ;
@@ -212,7 +219,7 @@barrier ();for (i =0 ; i < banks; i++) { -if (i <NR_SYSFS_BANKS && !bank[i]) +if (!bank[i])continue ; m.misc =0 ;
@@ -446,37 +453,54 @@/* * Initialize Machine Checks for a CPU. */ -static void mce_init (void *dummy) +static int mce_cap_init (void ) {u64 cap; -int i; +unsigned b;rdmsrl (MSR_IA32_MCG_CAP , cap); - banks = cap &0xff ; -if (banks >MCE_EXTENDED_BANK ) { - banks =MCE_EXTENDED_BANK ; -printk (KERN_INFO "MCE: warning: using only %d banks\n" , -MCE_EXTENDED_BANK ); + b = cap &0xff ; +if (b >MAX_NR_BANKS ) { +printk (KERN_WARNING +"MCE: Using only %u machine check banks out of %u\n" , +MAX_NR_BANKS , b); + b =MAX_NR_BANKS ; + } + +/* Don't support asymmetric configurations today */ +WARN_ON (banks !=0 && b != banks); + banks = b; +if (!bank) { + bank =kmalloc (banks *sizeof (u64),GFP_KERNEL ); +if (!bank) +return -ENOMEM ; +memset (bank,0xff , banks *sizeof (u64)); } +/* Use accurate RIP reporting if available. */ if ((cap & (1 <<9 )) && ((cap >>16 ) &0xff ) >=9 ) rip_msr =MSR_IA32_MCG_EIP ; +return 0 ; +} + +static void mce_init (void *dummy) +{ +u64 cap; +int i; +/* Log the machine checks left over from the previous reset. This also clears all registers */ do_machine_check (NULL , mce_bootlog ?-1 :-2 );set_in_cr4 (X86_CR4_MCE ); +rdmsrl (MSR_IA32_MCG_CAP , cap);if (cap &MCG_CTL_P )wrmsr (MSR_IA32_MCG_CTL ,0xffffffff ,0xffffffff );for (i =0 ; i < banks; i++) { -if (i <NR_SYSFS_BANKS ) -wrmsrl (MSR_IA32_MC0_CTL +4 *i, bank[i]); -else -wrmsrl (MSR_IA32_MC0_CTL +4 *i, ~0UL ); - +wrmsrl (MSR_IA32_MC0_CTL +4 *i, bank[i]);wrmsrl (MSR_IA32_MC0_STATUS +4 *i,0 ); } }
@@ -486,10 +510,10 @@ {/* This should be disabled by the BIOS, but isn't always */ if (c->x86_vendor ==X86_VENDOR_AMD ) { -if (c->x86 ==15 ) +if (c->x86 ==15 && banks >4 )/* disable GART TBL walk error reporting, which trips off incorrectly with the IOMMU & 3ware & Cerberus. */ -clear_bit (10 , &bank[4 ]); +clear_bit (10 , (unsigned long *)&bank[4 ]);if (c->x86 <=17 && mce_bootlog <0 )/* Lots of broken BIOS around that don't clear them by default and leave crap in there. Don't log. */
@@ -532,11 +556,15 @@*/ void __cpuinit mcheck_init (struct cpuinfo_x86 *c) { -mce_cpu_quirks (c); -if (!mce_available (c))return ; +if (mce_cap_init () <0 ) { + mce_dont_init =1 ; +return ; + } +mce_cpu_quirks (c); +mce_init (NULL );mce_cpu_features (c);mce_init_timer ();
@@ -819,16 +847,26 @@ } \ static SYSDEV_ATTR(name, 0644, show_ ## name, set_ ## name); -/* -* TBD should generate these dynamically based on number of available banks. -* Have only 6 contol banks in /sysfs until then. -*/ -ACCESSOR(bank0ctl,bank[0],mce_restart()) -ACCESSOR(bank1ctl,bank[1],mce_restart()) -ACCESSOR(bank2ctl,bank[2],mce_restart()) -ACCESSOR(bank3ctl,bank[3],mce_restart()) -ACCESSOR(bank4ctl,bank[4],mce_restart()) -ACCESSOR(bank5ctl,bank[5],mce_restart()) +static struct sysdev_attribute *bank_attrs; + +static ssize_t show_bank (struct sys_device *s,struct sysdev_attribute *attr, +char *buf) +{ +u64 b = bank[attr - bank_attrs]; +return sprintf (buf,"%Lx\n" , b); +} + +static ssize_t set_bank (struct sys_device *s,struct sysdev_attribute *attr, +const char *buf,size_t siz) +{ +char *end; +u64 new =simple_strtoull (buf, &end,0 ); +if (end == buf) +return -EINVAL ; + bank[attr - bank_attrs] = new; +mce_restart (); +return end-buf; +}static ssize_t show_trigger (struct sys_device *s,struct sysdev_attribute *attr,char *buf)
@@ -855,8 +893,6 @@ static SYSDEV_INT_ATTR(tolerant, 0644, tolerant); ACCESSOR(check_interval,check_interval,mce_restart()) static struct sysdev_attribute *mce_attributes[] = { - &attr_bank0ctl, &attr_bank1ctl, &attr_bank2ctl, - &attr_bank3ctl, &attr_bank4ctl, &attr_bank5ctl, &attr_tolerant.attr, &attr_check_interval, &attr_trigger,NULL };
@@ -886,11 +922,22 @@if (err) goto error; } +for (i =0 ; i < banks; i++) { + err =sysdev_create_file (&per_cpu (device_mce, cpu), + &bank_attrs[i]); +if (err) + goto error2; + }cpu_set (cpu, mce_device_initialized);return 0 ; +error2: +while (--i >=0 ) { +sysdev_remove_file (&per_cpu (device_mce, cpu), + &bank_attrs[i]); + } error: -while (i--) { +while (--i >=0 ) {sysdev_remove_file (&per_cpu (device_mce,cpu), mce_attributes[i]); }
@@ -909,6 +956,9 @@for (i =0 ; mce_attributes[i]; i++)sysdev_remove_file (&per_cpu (device_mce,cpu), mce_attributes[i]); +for (i =0 ; i < banks; i++) +sysdev_remove_file (&per_cpu (device_mce, cpu), + &bank_attrs[i]);sysdev_unregister (&per_cpu (device_mce,cpu));cpu_clear (cpu, mce_device_initialized); }
@@ -973,6 +1023,34 @@ .notifier_call = mce_cpu_callback, }; +static __init int mce_init_banks (void ) +{ +int i; + + bank_attrs =kzalloc (sizeof (struct sysdev_attribute ) * banks, +GFP_KERNEL ); +if (!bank_attrs) +return -ENOMEM ; + +for (i =0 ; i < banks; i++) { +struct sysdev_attribute *a = &bank_attrs[i]; + a->attr.name =kasprintf (GFP_KERNEL ,"bank%d" , i); +if (!a->attr.name) + goto nomem; + a->attr.mode =0644 ; + a->show = show_bank; + a->store = set_bank; + } +return 0 ; + +nomem: +while (--i >=0 ) +kfree (bank_attrs[i].attr.name); +kfree (bank_attrs); + bank_attrs =NULL ; +return -ENOMEM ; +} +static __init int mce_init_device (void ) {int err;
@@ -980,6 +1058,11 @@if (!mce_available (&boot_cpu_data))return -EIO ; + + err =mce_init_banks (); +if (err) +return err; + err =sysdev_class_register (&mce_sysclass);if (err)return err;
Signature · 1 line
[email protected] -- Speaking for myself only.