Skip to contentSkip to conversation

x86/ucode: Cleanup part 5/n

PATCH In review Andrew Cooper 2 participants 11 messages Apr 2, 2020 – Apr 3, 2020

lore
Share & tools
lore
#0 lore raw

cover x86/ucode: Cleanup part 5/n

Some fairly minor changes while I wait for more answers from vendors. I'm annoyed that I didn't spot patch 2 earlier, because it would have simplied some of the previous cleanup work. Oh well...

Andrew Cooper (5):
x86/ucode/intel: Remove CPUID from collect_cpu_info() x86/ucode: Drop ops->match_cpu()
x86/ucode: Don't try to cope with NULL pointers in apply_microcode() x86/ucode: Drop ops->free_patch()
x86/ucode: Simplify the ops->collect_cpu_info() API

 xen/arch/x86/cpu/microcode/amd.c     | 24 ++++--------------------
 xen/arch/x86/cpu/microcode/core.c    | 18 ++++++++----------
 xen/arch/x86/cpu/microcode/intel.c   | 29 ++++-------------------------
 xen/arch/x86/cpu/microcode/private.h | 18 ++++++------------
 4 files changed, 22 insertions(+), 67 deletions(-)
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/5 x86/ucode/intel: Remove CPUID from collect_cpu_info()

The CPUID instruction is expensive. No point executing it twice when once will do fine.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/microcode/intel.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
xen/arch/x86/cpu/microcode/intel.c +1 -3
index 72bd1ad0bc..f1e64e188b 100644
@@ -121,14 +121,12 @@ static int collect_cpu_info(struct cpu_signature *csig)
 
     memset(csig, 0, sizeof(*csig));
 
-    csig->sig = cpuid_eax(0x00000001);
-
     rdmsrl(MSR_IA32_PLATFORM_ID, msr_content);
     csig->pf = 1 << ((msr_content >> 50) & 7);
 
     wrmsrl(MSR_IA32_UCODE_REV, 0x0ULL);
     /* As documented in the SDM: Do a CPUID 1 here */
-    cpuid_eax(1);
+    csig->sig = cpuid_eax(1);
 
     /* get the current revision from MSR 0x8B */
     rdmsrl(MSR_IA32_UCODE_REV, msr_content);
Signature · 1 line
2.11.0
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] x86/ucode/intel: Remove CPUID from collect_cpu_info()

Quoted 4 lines · Andrew Cooper

The CPUID instruction is expensive. No point executing it twice when once will do fine.

albeit ...

Quoted 5 lines
xen/arch/x86/cpu/microcode/intel.c +0 -0
@@ -121,14 +121,12 @@ static int collect_cpu_info(struct cpu_signature *csig)
 
     memset(csig, 0, sizeof(*csig));

xen/arch/x86/cpu/microcode/intel.c static int collect_cpu_info(struct cpu_signature *csig) · line 129

Quoted 10 lines
xen/arch/x86/cpu/microcode/intel.c +1 -3
 
-    csig->sig = cpuid_eax(0x00000001);
-
     rdmsrl(MSR_IA32_PLATFORM_ID, msr_content);
     csig->pf = 1 << ((msr_content >> 50) & 7);
 
     wrmsrl(MSR_IA32_UCODE_REV, 0x0ULL);
     /* As documented in the SDM: Do a CPUID 1 here */
-    cpuid_eax(1);
+    csig->sig = cpuid_eax(1);

... with this, perhaps make the title say "remove one CPUID ..." or some such?

Jan

#3 lore raw

PATCH 2/5 x86/ucode: Drop ops->match_cpu()

In reply to Andrew Cooper

It turns out there are no callers of the hook(). The only callers are the local, which can easily be rearranged to use the appropriate internal helper.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/microcode/amd.c     |  8 +-------
 xen/arch/x86/cpu/microcode/intel.c   | 11 +----------
 xen/arch/x86/cpu/microcode/private.h |  6 ------
 3 files changed, 2 insertions(+), 23 deletions(-)
xen/arch/x86/cpu/microcode/amd.c +1 -7
index d4763ea776..c9656de55d 100644
@@ -188,11 +188,6 @@ static enum microcode_match_result microcode_fits(
     return NEW_UCODE;
 }
 
-static bool match_cpu(const struct microcode_patch *patch)
-{
-    return patch && (microcode_fits(patch) == NEW_UCODE);
-}
-
 static void free_patch(struct microcode_patch *patch)
 {
     xfree(patch);
@@ -227,7 +222,7 @@ static int apply_microcode(const struct microcode_patch *patch)
     if ( !patch )
         return -ENOENT;
 
-    if ( !match_cpu(patch) )
+    if ( microcode_fits(patch) != NEW_UCODE )
         return -EINVAL;
 
     if ( check_final_patch_levels(sig) )
@@ -428,5 +423,4 @@ const struct microcode_ops amd_ucode_ops = {
 #endif
     .free_patch                       = free_patch,
     .compare_patch                    = compare_patch,
-    .match_cpu                        = match_cpu,
 };
xen/arch/x86/cpu/microcode/intel.c +1 -10
index f1e64e188b..315fca9ff2 100644
@@ -245,14 +245,6 @@ static enum microcode_match_result microcode_update_match(
     return mc->rev > cpu_sig->rev ? NEW_UCODE : OLD_UCODE;
 }
 
-static bool match_cpu(const struct microcode_patch *patch)
-{
-    if ( !patch )
-        return false;
-
-    return microcode_update_match(patch) == NEW_UCODE;
-}
-
 static void free_patch(struct microcode_patch *patch)
 {
     xfree(patch);
@@ -281,7 +273,7 @@ static int apply_microcode(const struct microcode_patch *patch)
     if ( !patch )
         return -ENOENT;
 
-    if ( !match_cpu(patch) )
+    if ( microcode_update_match(patch) != NEW_UCODE )
         return -EINVAL;
 
     /* write microcode via MSR 0x79 */
@@ -369,5 +361,4 @@ const struct microcode_ops intel_ucode_ops = {
     .apply_microcode                  = apply_microcode,
     .free_patch                       = free_patch,
     .compare_patch                    = compare_patch,
-    .match_cpu                        = match_cpu,
 };
xen/arch/x86/cpu/microcode/private.h +0 -6
index df0d0852cd..d31bcf14b1 100644
@@ -60,12 +60,6 @@ struct microcode_ops {
     void (*free_patch)(struct microcode_patch *patch);
 
     /*
-     * Is the microcode patch applicable for the current CPU, and newer than
-     * the currently running patch?
-     */
-    bool (*match_cpu)(const struct microcode_patch *patch);
-
-    /*
      * Given two patches, are they both applicable to the current CPU, and is
      * new a higher revision than old?
      */
Signature · 1 line
2.11.0
#5 lore raw

PATCH 3/5 x86/ucode: Don't try to cope with NULL pointers in apply_microcode()

In reply to Andrew Cooper

No paths to apply_microcode() pass a NULL pointer, and other hooks don't tolerate one in the first place. We can expect the core logic not to pass us junk, so drop the checks.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/microcode/amd.c   | 3 ---
 xen/arch/x86/cpu/microcode/intel.c | 3 ---
 2 files changed, 6 deletions(-)
xen/arch/x86/cpu/microcode/amd.c +0 -3
index c9656de55d..0ca0e9a038 100644
@@ -219,9 +219,6 @@ static int apply_microcode(const struct microcode_patch *patch)
     struct cpu_signature *sig = &per_cpu(cpu_sig, cpu);
     uint32_t rev, old_rev = sig->rev;
 
-    if ( !patch )
-        return -ENOENT;
-
     if ( microcode_fits(patch) != NEW_UCODE )
         return -EINVAL;
 
xen/arch/x86/cpu/microcode/intel.c +0 -3
index 315fca9ff2..9cb077b583 100644
@@ -270,9 +270,6 @@ static int apply_microcode(const struct microcode_patch *patch)
     struct cpu_signature *sig = &this_cpu(cpu_sig);
     uint32_t rev, old_rev = sig->rev;
 
-    if ( !patch )
-        return -ENOENT;
-
     if ( microcode_update_match(patch) != NEW_UCODE )
         return -EINVAL;
 
Signature · 1 line
2.11.0
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] x86/ucode: Don't try to cope with NULL pointers in apply_microcode()

Quoted 5 lines · Andrew Cooper

No paths to apply_microcode() pass a NULL pointer, and other hooks don't tolerate one in the first place. We can expect the core logic not to pass us junk, so drop the checks.

#7 lore raw

PATCH 4/5 x86/ucode: Drop ops->free_patch()

In reply to Andrew Cooper

With the newly cleaned up vendor logic, each struct microcode_patch is a trivial object in memory with no dependent allocations.

This is unlikely to change moving forwards, and function pointers are expensive in the days of retpoline. Move the responsibility to xfree() back to common code. If the need does arise in the future, we can consider reintroducing the hook.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/microcode/amd.c     | 6 ------
 xen/arch/x86/cpu/microcode/core.c    | 4 ++--
 xen/arch/x86/cpu/microcode/intel.c   | 6 ------
 xen/arch/x86/cpu/microcode/private.h | 5 +----
 4 files changed, 3 insertions(+), 18 deletions(-)
xen/arch/x86/cpu/microcode/amd.c +0 -6
index 0ca0e9a038..e23bdef6f2 100644
@@ -188,11 +188,6 @@ static enum microcode_match_result microcode_fits(
     return NEW_UCODE;
 }
 
-static void free_patch(struct microcode_patch *patch)
-{
-    xfree(patch);
-}
-
 static enum microcode_match_result compare_header(
     const struct microcode_patch *new, const struct microcode_patch *old)
 {
@@ -418,6 +413,5 @@ const struct microcode_ops amd_ucode_ops = {
     .start_update                     = start_update,
     .end_update_percpu                = svm_host_osvw_init,
 #endif
-    .free_patch                       = free_patch,
     .compare_patch                    = compare_patch,
 };
xen/arch/x86/cpu/microcode/core.c +2 -2
index b3e5913d49..53e447ea9a 100644
@@ -243,9 +243,9 @@ static struct microcode_patch *parse_blob(const char *buf, size_t len)
     return NULL;
 }
 
-static void microcode_free_patch(struct microcode_patch *microcode_patch)
+static void microcode_free_patch(struct microcode_patch *patch)
 {
-    microcode_ops->free_patch(microcode_patch);
+    xfree(patch);
 }
 
 /* Return true if cache gets updated. Otherwise, return false */
xen/arch/x86/cpu/microcode/intel.c +0 -6
index 9cb077b583..29745ed55f 100644
@@ -245,11 +245,6 @@ static enum microcode_match_result microcode_update_match(
     return mc->rev > cpu_sig->rev ? NEW_UCODE : OLD_UCODE;
 }
 
-static void free_patch(struct microcode_patch *patch)
-{
-    xfree(patch);
-}
-
 static enum microcode_match_result compare_patch(
     const struct microcode_patch *new, const struct microcode_patch *old)
 {
@@ -356,6 +351,5 @@ const struct microcode_ops intel_ucode_ops = {
     .cpu_request_microcode            = cpu_request_microcode,
     .collect_cpu_info                 = collect_cpu_info,
     .apply_microcode                  = apply_microcode,
-    .free_patch                       = free_patch,
     .compare_patch                    = compare_patch,
 };
xen/arch/x86/cpu/microcode/private.h +1 -4
index d31bcf14b1..878f8d805f 100644
@@ -25,7 +25,7 @@ struct microcode_ops {
      *
      * If one is found, allocate and return a struct microcode_patch
      * encapsulating the appropriate microcode patch.  Does not alias the
-     * original buffer.
+     * original buffer.  Must be suitable to be freed with a single xfree().
      *
      * If one is not found, (nothing matches the current CPU), return NULL.
      * Also may return ERR_PTR(-err), e.g. bad container, out of memory.
@@ -56,9 +56,6 @@ struct microcode_ops {
      */
     void (*end_update_percpu)(void);
 
-    /* Free a patch previously allocated by cpu_request_microcode(). */
-    void (*free_patch)(struct microcode_patch *patch);
-
     /*
      * Given two patches, are they both applicable to the current CPU, and is
      * new a higher revision than old?
Signature · 1 line
2.11.0
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] x86/ucode: Drop ops->free_patch()

Quoted 9 lines · Andrew Cooper

With the newly cleaned up vendor logic, each struct microcode_patch is a trivial object in memory with no dependent allocations.

This is unlikely to change moving forwards, and function pointers are expensive in the days of retpoline. Move the responsibility to xfree() back to common code. If the need does arise in the future, we can consider reintroducing the hook.

Yet with the given argumentation, ...

xen/arch/x86/cpu/microcode/core.c static struct microcode_patch *parse_blob(const char *buf, size_t len) · line 249

Quoted 12 lines
xen/arch/x86/cpu/microcode/core.c +2 -2
@@ -243,9 +243,9 @@ static struct microcode_patch *parse_blob(const char *buf, size_t len)
     return NULL;
 }
 
-static void microcode_free_patch(struct microcode_patch *microcode_patch)
+static void microcode_free_patch(struct microcode_patch *patch)
 {
-    microcode_ops->free_patch(microcode_patch);
+    xfree(patch);
 }

... drop this wrapper as well? (R-b would also cover this)

Jan

#9 lore raw

PATCH 5/5 x86/ucode: Simplify the ops->collect_cpu_info() API

In reply to Andrew Cooper

All callers pass &this_cpu(cpu_sig) for the cpu_sig parameter, and all implementations unconditionally return 0. Simplify it to be void.

Drop the long-stale comment on the AMD side, whose counterpart in start_update() used to be "collect_cpu_info() doesn't fail so we're fine".

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/cpu/microcode/amd.c     |  7 +++----
 xen/arch/x86/cpu/microcode/core.c    | 14 ++++++--------
 xen/arch/x86/cpu/microcode/intel.c   |  5 ++---
 xen/arch/x86/cpu/microcode/private.h |  7 +++++--
 4 files changed, 16 insertions(+), 17 deletions(-)
xen/arch/x86/cpu/microcode/amd.c +3 -4
index e23bdef6f2..13bf9f4dee 100644
@@ -90,9 +90,10 @@ static struct {
     uint16_t id;
 } equiv __read_mostly;
 
-/* See comment in start_update() for cases when this routine fails */
-static int collect_cpu_info(struct cpu_signature *csig)
+static void collect_cpu_info(void)
 {
+    struct cpu_signature *csig = &this_cpu(cpu_sig);
+
     memset(csig, 0, sizeof(*csig));
 
     csig->sig = cpuid_eax(1);
@@ -100,8 +101,6 @@ static int collect_cpu_info(struct cpu_signature *csig)
 
     pr_debug("microcode: CPU%d collect_cpu_info: patch_id=%#x\n",
              smp_processor_id(), csig->rev);
-
-    return 0;
 }
 
 static bool_t verify_patch_size(uint32_t patch_size)
xen/arch/x86/cpu/microcode/core.c +6 -8
index 53e447ea9a..a220f908b8 100644
@@ -237,10 +237,9 @@ static const struct microcode_patch *nmi_patch = ZERO_BLOCK_PTR;
  */
 static struct microcode_patch *parse_blob(const char *buf, size_t len)
 {
-    if ( likely(!microcode_ops->collect_cpu_info(&this_cpu(cpu_sig))) )
-        return microcode_ops->cpu_request_microcode(buf, len);
+    microcode_ops->collect_cpu_info();
 
-    return NULL;
+    return microcode_ops->cpu_request_microcode(buf, len);
 }
 
 static void microcode_free_patch(struct microcode_patch *patch)
@@ -306,10 +305,9 @@ static bool wait_cpu_callout(unsigned int nr)
  */
 static int microcode_update_cpu(const struct microcode_patch *patch)
 {
-    int err = microcode_ops->collect_cpu_info(&this_cpu(cpu_sig));
+    int err;
 
-    if ( unlikely(err) )
-        return err;
+    microcode_ops->collect_cpu_info();
 
     spin_lock(&microcode_mutex);
     if ( patch )
@@ -737,7 +735,7 @@ int microcode_update_one(bool start_update)
     if ( !microcode_ops )
         return -EOPNOTSUPP;
 
-    microcode_ops->collect_cpu_info(&this_cpu(cpu_sig));
+    microcode_ops->collect_cpu_info();
 
     if ( start_update && microcode_ops->start_update )
     {
@@ -819,7 +817,7 @@ int __init early_microcode_init(void)
         return -ENODEV;
     }
 
-    microcode_ops->collect_cpu_info(&this_cpu(cpu_sig));
+    microcode_ops->collect_cpu_info();
 
     if ( ucode_mod.mod_end || ucode_blob.size )
         rc = early_microcode_update_cpu();
xen/arch/x86/cpu/microcode/intel.c +2 -3
index 29745ed55f..a9f4d6e829 100644
@@ -115,8 +115,9 @@ static bool signature_matches(const struct cpu_signature *cpu_sig,
     return cpu_sig->pf & ucode_pf;
 }
 
-static int collect_cpu_info(struct cpu_signature *csig)
+static void collect_cpu_info(void)
 {
+    struct cpu_signature *csig = &this_cpu(cpu_sig);
     uint64_t msr_content;
 
     memset(csig, 0, sizeof(*csig));
@@ -133,8 +134,6 @@ static int collect_cpu_info(struct cpu_signature *csig)
     csig->rev = (uint32_t)(msr_content >> 32);
     pr_debug("microcode: collect_cpu_info : sig=%#x, pf=%#x, rev=%#x\n",
              csig->sig, csig->pf, csig->rev);
-
-    return 0;
 }
 
 /*
xen/arch/x86/cpu/microcode/private.h +5 -2
index 878f8d805f..dc5c7a81ae 100644
@@ -33,8 +33,11 @@ struct microcode_ops {
     struct microcode_patch *(*cpu_request_microcode)(const void *buf,
                                                      size_t size);
 
-    /* Obtain microcode-relevant details for the current CPU. */
-    int (*collect_cpu_info)(struct cpu_signature *csig);
+    /*
+     * Obtain microcode-relevant details for the current CPU.  Results in
+     * per_cpu(cpu_sig).
+     */
+    void (*collect_cpu_info)(void);
 
     /*
      * Attempt to load the provided patch into the CPU.  Returns an error if
Signature · 1 line
2.11.0
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] x86/ucode: Simplify the ops->collect_cpu_info() API

Quoted 7 lines · Andrew Cooper

All callers pass &this_cpu(cpu_sig) for the cpu_sig parameter, and all implementations unconditionally return 0. Simplify it to be void.

Drop the long-stale comment on the AMD side, whose counterpart in start_update() used to be "collect_cpu_info() doesn't fail so we're fine".