| 1 |
From a5321aec6412b20b5ad15db2d6b916c05349dbff Mon Sep 17 00:00:00 2001 |
| 2 |
From: Ashok Raj <[email protected]> |
| 3 |
Date: Wed, 28 Feb 2018 11:28:46 +0100 |
| 4 |
Subject: x86/microcode: Synchronize late microcode loading |
| 5 |
|
| 6 |
Original idea by Ashok, completely rewritten by Borislav. |
| 7 |
|
| 8 |
Before you read any further: the early loading method is still the |
| 9 |
preferred one and you should always do that. The following patch is |
| 10 |
improving the late loading mechanism for long running jobs and cloud use |
| 11 |
cases. |
| 12 |
|
| 13 |
Gather all cores and serialize the microcode update on them by doing it |
| 14 |
one-by-one to make the late update process as reliable as possible and |
| 15 |
avoid potential issues caused by the microcode update. |
| 16 |
|
| 17 |
[ Borislav: Rewrite completely. ] |
| 18 |
|
| 19 |
Co-developed-by: Borislav Petkov <[email protected]> |
| 20 |
Signed-off-by: Ashok Raj <[email protected]> |
| 21 |
Signed-off-by: Borislav Petkov <[email protected]> |
| 22 |
Signed-off-by: Thomas Gleixner <[email protected]> |
| 23 |
Tested-by: Tom Lendacky <[email protected]> |
| 24 |
Tested-by: Ashok Raj <[email protected]> |
| 25 |
Reviewed-by: Tom Lendacky <[email protected]> |
| 26 |
Cc: Arjan Van De Ven <[email protected]> |
| 27 |
Link: https://lkml.kernel.org/r/[email protected] |
| 28 |
--- |
| 29 |
arch/x86/kernel/cpu/microcode/core.c | 118 +++++++++++++++++++++++++++-------- |
| 30 |
1 file changed, 92 insertions(+), 26 deletions(-) |
| 31 |
|
| 32 |
diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c |
| 33 |
index 5dd157d..70ecbc8 100644 |
| 34 |
--- a/arch/x86/kernel/cpu/microcode/core.c |
| 35 |
+++ b/arch/x86/kernel/cpu/microcode/core.c |
| 36 |
@@ -22,13 +22,16 @@ |
| 37 |
#define pr_fmt(fmt) "microcode: " fmt |
| 38 |
|
| 39 |
#include <linux/platform_device.h> |
| 40 |
+#include <linux/stop_machine.h> |
| 41 |
#include <linux/syscore_ops.h> |
| 42 |
#include <linux/miscdevice.h> |
| 43 |
#include <linux/capability.h> |
| 44 |
#include <linux/firmware.h> |
| 45 |
#include <linux/kernel.h> |
| 46 |
+#include <linux/delay.h> |
| 47 |
#include <linux/mutex.h> |
| 48 |
#include <linux/cpu.h> |
| 49 |
+#include <linux/nmi.h> |
| 50 |
#include <linux/fs.h> |
| 51 |
#include <linux/mm.h> |
| 52 |
|
| 53 |
@@ -64,6 +67,11 @@ LIST_HEAD(microcode_cache); |
| 54 |
*/ |
| 55 |
static DEFINE_MUTEX(microcode_mutex); |
| 56 |
|
| 57 |
+/* |
| 58 |
+ * Serialize late loading so that CPUs get updated one-by-one. |
| 59 |
+ */ |
| 60 |
+static DEFINE_SPINLOCK(update_lock); |
| 61 |
+ |
| 62 |
struct ucode_cpu_info ucode_cpu_info[NR_CPUS]; |
| 63 |
|
| 64 |
struct cpu_info_ctx { |
| 65 |
@@ -486,6 +494,19 @@ static void __exit microcode_dev_exit(void) |
| 66 |
/* fake device for request_firmware */ |
| 67 |
static struct platform_device *microcode_pdev; |
| 68 |
|
| 69 |
+/* |
| 70 |
+ * Late loading dance. Why the heavy-handed stomp_machine effort? |
| 71 |
+ * |
| 72 |
+ * - HT siblings must be idle and not execute other code while the other sibling |
| 73 |
+ * is loading microcode in order to avoid any negative interactions caused by |
| 74 |
+ * the loading. |
| 75 |
+ * |
| 76 |
+ * - In addition, microcode update on the cores must be serialized until this |
| 77 |
+ * requirement can be relaxed in the future. Right now, this is conservative |
| 78 |
+ * and good. |
| 79 |
+ */ |
| 80 |
+#define SPINUNIT 100 /* 100 nsec */ |
| 81 |
+ |
| 82 |
static int check_online_cpus(void) |
| 83 |
{ |
| 84 |
if (num_online_cpus() == num_present_cpus()) |
| 85 |
@@ -496,23 +517,85 @@ static int check_online_cpus(void) |
| 86 |
return -EINVAL; |
| 87 |
} |
| 88 |
|
| 89 |
-static enum ucode_state reload_for_cpu(int cpu) |
| 90 |
+static atomic_t late_cpus; |
| 91 |
+ |
| 92 |
+/* |
| 93 |
+ * Returns: |
| 94 |
+ * < 0 - on error |
| 95 |
+ * 0 - no update done |
| 96 |
+ * 1 - microcode was updated |
| 97 |
+ */ |
| 98 |
+static int __reload_late(void *info) |
| 99 |
{ |
| 100 |
- struct ucode_cpu_info *uci = ucode_cpu_info + cpu; |
| 101 |
+ unsigned int timeout = NSEC_PER_SEC; |
| 102 |
+ int all_cpus = num_online_cpus(); |
| 103 |
+ int cpu = smp_processor_id(); |
| 104 |
+ enum ucode_state err; |
| 105 |
+ int ret = 0; |
| 106 |
|
| 107 |
- if (!uci->valid) |
| 108 |
- return UCODE_OK; |
| 109 |
+ atomic_dec(&late_cpus); |
| 110 |
+ |
| 111 |
+ /* |
| 112 |
+ * Wait for all CPUs to arrive. A load will not be attempted unless all |
| 113 |
+ * CPUs show up. |
| 114 |
+ * */ |
| 115 |
+ while (atomic_read(&late_cpus)) { |
| 116 |
+ if (timeout < SPINUNIT) { |
| 117 |
+ pr_err("Timeout while waiting for CPUs rendezvous, remaining: %d\n", |
| 118 |
+ atomic_read(&late_cpus)); |
| 119 |
+ return -1; |
| 120 |
+ } |
| 121 |
+ |
| 122 |
+ ndelay(SPINUNIT); |
| 123 |
+ timeout -= SPINUNIT; |
| 124 |
+ |
| 125 |
+ touch_nmi_watchdog(); |
| 126 |
+ } |
| 127 |
+ |
| 128 |
+ spin_lock(&update_lock); |
| 129 |
+ apply_microcode_local(&err); |
| 130 |
+ spin_unlock(&update_lock); |
| 131 |
+ |
| 132 |
+ if (err > UCODE_NFOUND) { |
| 133 |
+ pr_warn("Error reloading microcode on CPU %d\n", cpu); |
| 134 |
+ ret = -1; |
| 135 |
+ } else if (err == UCODE_UPDATED) { |
| 136 |
+ ret = 1; |
| 137 |
+ } |
| 138 |
|
| 139 |
- return apply_microcode_on_target(cpu); |
| 140 |
+ atomic_inc(&late_cpus); |
| 141 |
+ |
| 142 |
+ while (atomic_read(&late_cpus) != all_cpus) |
| 143 |
+ cpu_relax(); |
| 144 |
+ |
| 145 |
+ return ret; |
| 146 |
+} |
| 147 |
+ |
| 148 |
+/* |
| 149 |
+ * Reload microcode late on all CPUs. Wait for a sec until they |
| 150 |
+ * all gather together. |
| 151 |
+ */ |
| 152 |
+static int microcode_reload_late(void) |
| 153 |
+{ |
| 154 |
+ int ret; |
| 155 |
+ |
| 156 |
+ atomic_set(&late_cpus, num_online_cpus()); |
| 157 |
+ |
| 158 |
+ ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); |
| 159 |
+ if (ret < 0) |
| 160 |
+ return ret; |
| 161 |
+ else if (ret > 0) |
| 162 |
+ microcode_check(); |
| 163 |
+ |
| 164 |
+ return ret; |
| 165 |
} |
| 166 |
|
| 167 |
static ssize_t reload_store(struct device *dev, |
| 168 |
struct device_attribute *attr, |
| 169 |
const char *buf, size_t size) |
| 170 |
{ |
| 171 |
- int cpu, bsp = boot_cpu_data.cpu_index; |
| 172 |
enum ucode_state tmp_ret = UCODE_OK; |
| 173 |
- bool do_callback = false; |
| 174 |
+ int bsp = boot_cpu_data.cpu_index; |
| 175 |
unsigned long val; |
| 176 |
ssize_t ret = 0; |
| 177 |
|
| 178 |
@@ -534,30 +617,13 @@ static ssize_t reload_store(struct device *dev, |
| 179 |
goto put; |
| 180 |
|
| 181 |
mutex_lock(µcode_mutex); |
| 182 |
- |
| 183 |
- for_each_online_cpu(cpu) { |
| 184 |
- tmp_ret = reload_for_cpu(cpu); |
| 185 |
- if (tmp_ret > UCODE_NFOUND) { |
| 186 |
- pr_warn("Error reloading microcode on CPU %d\n", cpu); |
| 187 |
- |
| 188 |
- /* set retval for the first encountered reload error */ |
| 189 |
- if (!ret) |
| 190 |
- ret = -EINVAL; |
| 191 |
- } |
| 192 |
- |
| 193 |
- if (tmp_ret == UCODE_UPDATED) |
| 194 |
- do_callback = true; |
| 195 |
- } |
| 196 |
- |
| 197 |
- if (!ret && do_callback) |
| 198 |
- microcode_check(); |
| 199 |
- |
| 200 |
+ ret = microcode_reload_late(); |
| 201 |
mutex_unlock(µcode_mutex); |
| 202 |
|
| 203 |
put: |
| 204 |
put_online_cpus(); |
| 205 |
|
| 206 |
- if (!ret) |
| 207 |
+ if (ret >= 0) |
| 208 |
ret = size; |
| 209 |
|
| 210 |
return ret; |
| 211 |
-- |
| 212 |
cgit v1.1 |
| 213 |
|