| 1 |
From 2613f36ed965d0e5a595a1d931fd3b480e82d6fd Mon Sep 17 00:00:00 2001 |
| 2 |
From: Borislav Petkov <[email protected]> |
| 3 |
Date: Wed, 14 Mar 2018 19:36:14 +0100 |
| 4 |
Subject: x86/microcode: Attempt late loading only when new microcode is |
| 5 |
present |
| 6 |
|
| 7 |
Return UCODE_NEW from the scanning functions to denote that new microcode |
| 8 |
was found and only then attempt the expensive synchronization dance. |
| 9 |
|
| 10 |
Reported-by: Emanuel Czirai <[email protected]> |
| 11 |
Signed-off-by: Borislav Petkov <[email protected]> |
| 12 |
Signed-off-by: Thomas Gleixner <[email protected]> |
| 13 |
Tested-by: Emanuel Czirai <[email protected]> |
| 14 |
Tested-by: Ashok Raj <[email protected]> |
| 15 |
Tested-by: Tom Lendacky <[email protected]> |
| 16 |
Link: https://lkml.kernel.org/r/[email protected] |
| 17 |
--- |
| 18 |
arch/x86/include/asm/microcode.h | 1 + |
| 19 |
arch/x86/kernel/cpu/microcode/amd.c | 34 +++++++++++++++++++++------------- |
| 20 |
arch/x86/kernel/cpu/microcode/core.c | 8 +++----- |
| 21 |
arch/x86/kernel/cpu/microcode/intel.c | 4 +++- |
| 22 |
4 files changed, 28 insertions(+), 19 deletions(-) |
| 23 |
|
| 24 |
diff --git a/arch/x86/include/asm/microcode.h b/arch/x86/include/asm/microcode.h |
| 25 |
index 7fb1047..6cf0e4c 100644 |
| 26 |
--- a/arch/x86/include/asm/microcode.h |
| 27 |
+++ b/arch/x86/include/asm/microcode.h |
| 28 |
@@ -39,6 +39,7 @@ struct device; |
| 29 |
|
| 30 |
enum ucode_state { |
| 31 |
UCODE_OK = 0, |
| 32 |
+ UCODE_NEW, |
| 33 |
UCODE_UPDATED, |
| 34 |
UCODE_NFOUND, |
| 35 |
UCODE_ERROR, |
| 36 |
diff --git a/arch/x86/kernel/cpu/microcode/amd.c b/arch/x86/kernel/cpu/microcode/amd.c |
| 37 |
index a998e1a..4817992 100644 |
| 38 |
--- a/arch/x86/kernel/cpu/microcode/amd.c |
| 39 |
+++ b/arch/x86/kernel/cpu/microcode/amd.c |
| 40 |
@@ -339,7 +339,7 @@ int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax) |
| 41 |
return -EINVAL; |
| 42 |
|
| 43 |
ret = load_microcode_amd(true, x86_family(cpuid_1_eax), desc.data, desc.size); |
| 44 |
- if (ret != UCODE_OK) |
| 45 |
+ if (ret > UCODE_UPDATED) |
| 46 |
return -EINVAL; |
| 47 |
|
| 48 |
return 0; |
| 49 |
@@ -683,27 +683,35 @@ static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, |
| 50 |
static enum ucode_state |
| 51 |
load_microcode_amd(bool save, u8 family, const u8 *data, size_t size) |
| 52 |
{ |
| 53 |
+ struct ucode_patch *p; |
| 54 |
enum ucode_state ret; |
| 55 |
|
| 56 |
/* free old equiv table */ |
| 57 |
free_equiv_cpu_table(); |
| 58 |
|
| 59 |
ret = __load_microcode_amd(family, data, size); |
| 60 |
- |
| 61 |
- if (ret != UCODE_OK) |
| 62 |
+ if (ret != UCODE_OK) { |
| 63 |
cleanup(); |
| 64 |
+ return ret; |
| 65 |
+ } |
| 66 |
|
| 67 |
-#ifdef CONFIG_X86_32 |
| 68 |
- /* save BSP's matching patch for early load */ |
| 69 |
- if (save) { |
| 70 |
- struct ucode_patch *p = find_patch(0); |
| 71 |
- if (p) { |
| 72 |
- memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); |
| 73 |
- memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), |
| 74 |
- PATCH_MAX_SIZE)); |
| 75 |
- } |
| 76 |
+ p = find_patch(0); |
| 77 |
+ if (!p) { |
| 78 |
+ return ret; |
| 79 |
+ } else { |
| 80 |
+ if (boot_cpu_data.microcode == p->patch_id) |
| 81 |
+ return ret; |
| 82 |
+ |
| 83 |
+ ret = UCODE_NEW; |
| 84 |
} |
| 85 |
-#endif |
| 86 |
+ |
| 87 |
+ /* save BSP's matching patch for early load */ |
| 88 |
+ if (!save) |
| 89 |
+ return ret; |
| 90 |
+ |
| 91 |
+ memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); |
| 92 |
+ memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data), PATCH_MAX_SIZE)); |
| 93 |
+ |
| 94 |
return ret; |
| 95 |
} |
| 96 |
|
| 97 |
diff --git a/arch/x86/kernel/cpu/microcode/core.c b/arch/x86/kernel/cpu/microcode/core.c |
| 98 |
index 70ecbc8..9f0fe5b 100644 |
| 99 |
--- a/arch/x86/kernel/cpu/microcode/core.c |
| 100 |
+++ b/arch/x86/kernel/cpu/microcode/core.c |
| 101 |
@@ -607,7 +607,7 @@ static ssize_t reload_store(struct device *dev, |
| 102 |
return size; |
| 103 |
|
| 104 |
tmp_ret = microcode_ops->request_microcode_fw(bsp, µcode_pdev->dev, true); |
| 105 |
- if (tmp_ret != UCODE_OK) |
| 106 |
+ if (tmp_ret != UCODE_NEW) |
| 107 |
return size; |
| 108 |
|
| 109 |
get_online_cpus(); |
| 110 |
@@ -691,10 +691,8 @@ static enum ucode_state microcode_init_cpu(int cpu, bool refresh_fw) |
| 111 |
if (system_state != SYSTEM_RUNNING) |
| 112 |
return UCODE_NFOUND; |
| 113 |
|
| 114 |
- ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev, |
| 115 |
- refresh_fw); |
| 116 |
- |
| 117 |
- if (ustate == UCODE_OK) { |
| 118 |
+ ustate = microcode_ops->request_microcode_fw(cpu, µcode_pdev->dev, refresh_fw); |
| 119 |
+ if (ustate == UCODE_NEW) { |
| 120 |
pr_debug("CPU%d updated upon init\n", cpu); |
| 121 |
apply_microcode_on_target(cpu); |
| 122 |
} |
| 123 |
diff --git a/arch/x86/kernel/cpu/microcode/intel.c b/arch/x86/kernel/cpu/microcode/intel.c |
| 124 |
index 2aded9d..32b8e57 100644 |
| 125 |
--- a/arch/x86/kernel/cpu/microcode/intel.c |
| 126 |
+++ b/arch/x86/kernel/cpu/microcode/intel.c |
| 127 |
@@ -862,6 +862,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size, |
| 128 |
unsigned int leftover = size; |
| 129 |
unsigned int curr_mc_size = 0, new_mc_size = 0; |
| 130 |
unsigned int csig, cpf; |
| 131 |
+ enum ucode_state ret = UCODE_OK; |
| 132 |
|
| 133 |
while (leftover) { |
| 134 |
struct microcode_header_intel mc_header; |
| 135 |
@@ -903,6 +904,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size, |
| 136 |
new_mc = mc; |
| 137 |
new_mc_size = mc_size; |
| 138 |
mc = NULL; /* trigger new vmalloc */ |
| 139 |
+ ret = UCODE_NEW; |
| 140 |
} |
| 141 |
|
| 142 |
ucode_ptr += mc_size; |
| 143 |
@@ -932,7 +934,7 @@ static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size, |
| 144 |
pr_debug("CPU%d found a matching microcode update with version 0x%x (current=0x%x)\n", |
| 145 |
cpu, new_rev, uci->cpu_sig.rev); |
| 146 |
|
| 147 |
- return UCODE_OK; |
| 148 |
+ return ret; |
| 149 |
} |
| 150 |
|
| 151 |
static int get_ucode_fw(void *to, const void *from, size_t n) |
| 152 |
-- |
| 153 |
cgit v1.1 |
| 154 |
|