Skip to content

Commit 7eae17c

Browse files
aeglbp3tk0v
authored andcommitted
x86/mce: Add per-bank CMCI storm mitigation
This is the core functionality to track CMCI storms at the machine check bank granularity. Subsequent patches will add the vendor specific hooks to supply input to the storm detection and take actions on the start/end of a storm. machine_check_poll() is called both by the CMCI interrupt code, and for periodic polls from a timer. Add a hook in this routine to maintain a bitmap history for each bank showing whether the bank logged an corrected error or not each time it is polled. In normal operation the interval between polls of these banks determines how far to shift the history. The 64 bit width corresponds to about one second. When a storm is observed a CPU vendor specific action is taken to reduce or stop CMCI from the bank that is the source of the storm. The bank is added to the bitmap of banks for this CPU to poll. The polling rate is increased to once per second. During a storm each bit in the history indicates the status of the bank each time it is polled. Thus the history covers just over a minute. Declare a storm for that bank if the number of corrected interrupts seen in that history is above some threshold (defined as 5 in this series, could be tuned later if there is data to suggest a better value). A storm on a bank ends if enough consecutive polls of the bank show no corrected errors (defined as 30, may also change). That calls the CPU vendor specific function to revert to normal operational mode, and changes the polling rate back to the default. [ bp: Massage. ] Signed-off-by: Tony Luck <tony.luck@intel.com> Signed-off-by: Borislav Petkov (AMD) <bp@alien8.de> Link: https://lore.kernel.org/r/20231115195450.12963-3-tony.luck@intel.com
1 parent 3ed57b4 commit 7eae17c

File tree

3 files changed

+194
-9
lines changed

3 files changed

+194
-9
lines changed

arch/x86/kernel/cpu/mce/core.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,16 @@ bool machine_check_poll(enum mcp_flags flags, mce_banks_t *b)
686686
barrier();
687687
m.status = mce_rdmsrl(mca_msr_reg(i, MCA_STATUS));
688688

689+
/*
690+
* Update storm tracking here, before checking for the
691+
* MCI_STATUS_VAL bit. Valid corrected errors count
692+
* towards declaring, or maintaining, storm status. No
693+
* error in a bank counts towards avoiding, or ending,
694+
* storm status.
695+
*/
696+
if (!mca_cfg.cmci_disabled)
697+
mce_track_storm(&m);
698+
689699
/* If this entry is not valid, ignore it */
690700
if (!(m.status & MCI_STATUS_VAL))
691701
continue;
@@ -1658,22 +1668,29 @@ static void mce_timer_fn(struct timer_list *t)
16581668
else
16591669
iv = min(iv * 2, round_jiffies_relative(check_interval * HZ));
16601670

1661-
__this_cpu_write(mce_next_interval, iv);
1662-
__start_timer(t, iv);
1671+
if (mce_get_storm_mode()) {
1672+
__start_timer(t, HZ);
1673+
} else {
1674+
__this_cpu_write(mce_next_interval, iv);
1675+
__start_timer(t, iv);
1676+
}
16631677
}
16641678

16651679
/*
1666-
* Ensure that the timer is firing in @interval from now.
1680+
* When a storm starts on any bank on this CPU, switch to polling
1681+
* once per second. When the storm ends, revert to the default
1682+
* polling interval.
16671683
*/
1668-
void mce_timer_kick(unsigned long interval)
1684+
void mce_timer_kick(bool storm)
16691685
{
16701686
struct timer_list *t = this_cpu_ptr(&mce_timer);
1671-
unsigned long iv = __this_cpu_read(mce_next_interval);
16721687

1673-
__start_timer(t, interval);
1688+
mce_set_storm_mode(storm);
16741689

1675-
if (interval < iv)
1676-
__this_cpu_write(mce_next_interval, interval);
1690+
if (storm)
1691+
__start_timer(t, HZ);
1692+
else
1693+
__this_cpu_write(mce_next_interval, check_interval * HZ);
16771694
}
16781695

16791696
/* Must not be called in IRQ context where del_timer_sync() can deadlock */

arch/x86/kernel/cpu/mce/internal.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,63 @@ static inline bool intel_filter_mce(struct mce *m) { return false; }
5656
static inline bool intel_mce_usable_address(struct mce *m) { return false; }
5757
#endif
5858

59-
void mce_timer_kick(unsigned long interval);
59+
void mce_timer_kick(bool storm);
60+
61+
#ifdef CONFIG_X86_MCE_THRESHOLD
62+
void cmci_storm_begin(unsigned int bank);
63+
void cmci_storm_end(unsigned int bank);
64+
void mce_track_storm(struct mce *mce);
65+
void mce_inherit_storm(unsigned int bank);
66+
bool mce_get_storm_mode(void);
67+
void mce_set_storm_mode(bool storm);
68+
#else
69+
static inline void cmci_storm_begin(unsigned int bank) {}
70+
static inline void cmci_storm_end(unsigned int bank) {}
71+
static inline void mce_track_storm(struct mce *mce) {}
72+
static inline void mce_inherit_storm(unsigned int bank) {}
73+
static inline bool mce_get_storm_mode(void) { return false; }
74+
static inline void mce_set_storm_mode(bool storm) {}
75+
#endif
76+
77+
/*
78+
* history: Bitmask tracking errors occurrence. Each set bit
79+
* represents an error seen.
80+
*
81+
* timestamp: Last time (in jiffies) that the bank was polled.
82+
* in_storm_mode: Is this bank in storm mode?
83+
* poll_only: Bank does not support CMCI, skip storm tracking.
84+
*/
85+
struct storm_bank {
86+
u64 history;
87+
u64 timestamp;
88+
bool in_storm_mode;
89+
bool poll_only;
90+
};
91+
92+
#define NUM_HISTORY_BITS (sizeof(u64) * BITS_PER_BYTE)
93+
94+
/* How many errors within the history buffer mark the start of a storm. */
95+
#define STORM_BEGIN_THRESHOLD 5
96+
97+
/*
98+
* How many polls of machine check bank without an error before declaring
99+
* the storm is over. Since it is tracked by the bitmasks in the history
100+
* field of struct storm_bank the mask is 30 bits [0 ... 29].
101+
*/
102+
#define STORM_END_POLL_THRESHOLD 29
103+
104+
/*
105+
* banks: per-cpu, per-bank details
106+
* stormy_bank_count: count of MC banks in storm state
107+
* poll_mode: CPU is in poll mode
108+
*/
109+
struct mca_storm_desc {
110+
struct storm_bank banks[MAX_NR_BANKS];
111+
u8 stormy_bank_count;
112+
bool poll_mode;
113+
};
114+
115+
DECLARE_PER_CPU(struct mca_storm_desc, storm_desc);
60116

61117
#ifdef CONFIG_ACPI_APEI
62118
int apei_write_mce(struct mce *m);

arch/x86/kernel/cpu/mce/threshold.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,115 @@ DEFINE_IDTENTRY_SYSVEC(sysvec_threshold)
2929
trace_threshold_apic_exit(THRESHOLD_APIC_VECTOR);
3030
apic_eoi();
3131
}
32+
33+
DEFINE_PER_CPU(struct mca_storm_desc, storm_desc);
34+
35+
void mce_inherit_storm(unsigned int bank)
36+
{
37+
struct mca_storm_desc *storm = this_cpu_ptr(&storm_desc);
38+
39+
/*
40+
* Previous CPU owning this bank had put it into storm mode,
41+
* but the precise history of that storm is unknown. Assume
42+
* the worst (all recent polls of the bank found a valid error
43+
* logged). This will avoid the new owner prematurely declaring
44+
* the storm has ended.
45+
*/
46+
storm->banks[bank].history = ~0ull;
47+
storm->banks[bank].timestamp = jiffies;
48+
}
49+
50+
bool mce_get_storm_mode(void)
51+
{
52+
return __this_cpu_read(storm_desc.poll_mode);
53+
}
54+
55+
void mce_set_storm_mode(bool storm)
56+
{
57+
__this_cpu_write(storm_desc.poll_mode, storm);
58+
}
59+
60+
static void mce_handle_storm(unsigned int bank, bool on)
61+
{
62+
switch (boot_cpu_data.x86_vendor) {
63+
}
64+
}
65+
66+
void cmci_storm_begin(unsigned int bank)
67+
{
68+
struct mca_storm_desc *storm = this_cpu_ptr(&storm_desc);
69+
70+
__set_bit(bank, this_cpu_ptr(mce_poll_banks));
71+
storm->banks[bank].in_storm_mode = true;
72+
73+
/*
74+
* If this is the first bank on this CPU to enter storm mode
75+
* start polling.
76+
*/
77+
if (++storm->stormy_bank_count == 1)
78+
mce_timer_kick(true);
79+
}
80+
81+
void cmci_storm_end(unsigned int bank)
82+
{
83+
struct mca_storm_desc *storm = this_cpu_ptr(&storm_desc);
84+
85+
__clear_bit(bank, this_cpu_ptr(mce_poll_banks));
86+
storm->banks[bank].history = 0;
87+
storm->banks[bank].in_storm_mode = false;
88+
89+
/* If no banks left in storm mode, stop polling. */
90+
if (!this_cpu_dec_return(storm_desc.stormy_bank_count))
91+
mce_timer_kick(false);
92+
}
93+
94+
void mce_track_storm(struct mce *mce)
95+
{
96+
struct mca_storm_desc *storm = this_cpu_ptr(&storm_desc);
97+
unsigned long now = jiffies, delta;
98+
unsigned int shift = 1;
99+
u64 history = 0;
100+
101+
/* No tracking needed for banks that do not support CMCI */
102+
if (storm->banks[mce->bank].poll_only)
103+
return;
104+
105+
/*
106+
* When a bank is in storm mode it is polled once per second and
107+
* the history mask will record about the last minute of poll results.
108+
* If it is not in storm mode, then the bank is only checked when
109+
* there is a CMCI interrupt. Check how long it has been since
110+
* this bank was last checked, and adjust the amount of "shift"
111+
* to apply to history.
112+
*/
113+
if (!storm->banks[mce->bank].in_storm_mode) {
114+
delta = now - storm->banks[mce->bank].timestamp;
115+
shift = (delta + HZ) / HZ;
116+
}
117+
118+
/* If it has been a long time since the last poll, clear history. */
119+
if (shift < NUM_HISTORY_BITS)
120+
history = storm->banks[mce->bank].history << shift;
121+
122+
storm->banks[mce->bank].timestamp = now;
123+
124+
/* History keeps track of corrected errors. VAL=1 && UC=0 */
125+
if ((mce->status & MCI_STATUS_VAL) && mce_is_correctable(mce))
126+
history |= 1;
127+
128+
storm->banks[mce->bank].history = history;
129+
130+
if (storm->banks[mce->bank].in_storm_mode) {
131+
if (history & GENMASK_ULL(STORM_END_POLL_THRESHOLD, 0))
132+
return;
133+
printk_deferred(KERN_NOTICE "CPU%d BANK%d CMCI storm subsided\n", smp_processor_id(), mce->bank);
134+
mce_handle_storm(mce->bank, false);
135+
cmci_storm_end(mce->bank);
136+
} else {
137+
if (hweight64(history) < STORM_BEGIN_THRESHOLD)
138+
return;
139+
printk_deferred(KERN_NOTICE "CPU%d BANK%d CMCI storm detected\n", smp_processor_id(), mce->bank);
140+
mce_handle_storm(mce->bank, true);
141+
cmci_storm_begin(mce->bank);
142+
}
143+
}

0 commit comments

Comments
 (0)