Commit 871b72dd authored by Dmitry Adamushko's avatar Dmitry Adamushko Committed by Ingo Molnar

x86: microcode: use smp_call_function_single instead of set_cpus_allowed,...

x86: microcode: use smp_call_function_single instead of set_cpus_allowed, cleanup of synchronization logic

* Solve issues described in 6f66cbc6
  in a way that doesn't resort to set_cpus_allowed();

* in fact, only collect_cpu_info and apply_microcode callbacks
  must run on a target cpu, others will do just fine on any other.
  smp_call_function_single() (as suggested by Ingo) is used to run
  these callbacks on a target cpu.

* cleanup of synchronization logic of the 'microcode_core' part

  The generic 'microcode_core' part guarantees that only a single cpu
  (be it a full-fledged cpu, one of the cores or HT)
  is being updated at any particular moment of time.

  In general, there is no need for any additional sync. mechanism in
  arch-specific parts (the patch removes existing spinlocks).

  See also the "Synchronization" section in microcode_core.c.

* return -EINVAL instead of -1 (which is translated into -EPERM) in
  microcode_write(), reload_cpu() and mc_sysdev_add(). Other suggestions
  for an error code?

* use 'enum ucode_state' as return value of request_microcode_{fw, user}
  to gain more flexibility by distinguishing between real error cases
  and situations when an appropriate ucode was not found (which is not an
  error per-se).

* some minor cleanups

Thanks a lot to Hugh Dickins for review/suggestions/testing!

   Reference: http://marc.info/?l=linux-kernel&m=124025889012541&w=2

[ Impact: refactor and clean up microcode driver locking code ]
Signed-off-by: default avatarDmitry Adamushko <dmitry.adamushko@gmail.com>
Acked-by: default avatarHugh Dickins <hugh@veritas.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Cc: Andreas Herrmann <andreas.herrmann3@amd.com>
Cc: Peter Oruba <peter.oruba@amd.com>
Cc: Arjan van de Ven <arjan@infradead.org>
LKML-Reference: <1242078507.5560.9.camel@earth>
[ did some more cleanups ]
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
 arch/x86/include/asm/microcode.h  |   25 ++
 arch/x86/kernel/microcode_amd.c   |   58 ++----
 arch/x86/kernel/microcode_core.c  |  326 +++++++++++++++++++++-----------------
 arch/x86/kernel/microcode_intel.c |   92 +++-------
 4 files changed, 261 insertions(+), 240 deletions(-)

(~20 new comment lines)
parent a4d7749b
...@@ -9,14 +9,25 @@ struct cpu_signature { ...@@ -9,14 +9,25 @@ struct cpu_signature {
struct device; struct device;
enum ucode_state { UCODE_ERROR, UCODE_OK, UCODE_NFOUND };
struct microcode_ops { struct microcode_ops {
int (*request_microcode_user) (int cpu, const void __user *buf, size_t size); enum ucode_state (*request_microcode_user) (int cpu,
int (*request_microcode_fw) (int cpu, struct device *device); const void __user *buf, size_t size);
void (*apply_microcode) (int cpu); enum ucode_state (*request_microcode_fw) (int cpu,
struct device *device);
int (*collect_cpu_info) (int cpu, struct cpu_signature *csig);
void (*microcode_fini_cpu) (int cpu); void (*microcode_fini_cpu) (int cpu);
/*
* The generic 'microcode_core' part guarantees that
* the callbacks below run on a target cpu when they
* are being called.
* See also the "Synchronization" section in microcode_core.c.
*/
int (*apply_microcode) (int cpu);
int (*collect_cpu_info) (int cpu, struct cpu_signature *csig);
}; };
struct ucode_cpu_info { struct ucode_cpu_info {
......
...@@ -13,25 +13,13 @@ ...@@ -13,25 +13,13 @@
* Licensed under the terms of the GNU General Public * Licensed under the terms of the GNU General Public
* License version 2. See file COPYING for details. * License version 2. See file COPYING for details.
*/ */
#include <linux/platform_device.h>
#include <linux/capability.h>
#include <linux/miscdevice.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/spinlock.h>
#include <linux/cpumask.h>
#include <linux/pci_ids.h> #include <linux/pci_ids.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/vmalloc.h> #include <linux/vmalloc.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/pci.h> #include <linux/pci.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/microcode.h> #include <asm/microcode.h>
#include <asm/processor.h> #include <asm/processor.h>
...@@ -79,9 +67,6 @@ struct microcode_amd { ...@@ -79,9 +67,6 @@ struct microcode_amd {
#define UCODE_CONTAINER_SECTION_HDR 8 #define UCODE_CONTAINER_SECTION_HDR 8
#define UCODE_CONTAINER_HEADER_SIZE 12 #define UCODE_CONTAINER_HEADER_SIZE 12
/* serialize access to the physical write */
static DEFINE_SPINLOCK(microcode_update_lock);
static struct equiv_cpu_entry *equiv_cpu_table; static struct equiv_cpu_entry *equiv_cpu_table;
static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig) static int collect_cpu_info_amd(int cpu, struct cpu_signature *csig)
...@@ -144,9 +129,8 @@ static int get_matching_microcode(int cpu, void *mc, int rev) ...@@ -144,9 +129,8 @@ static int get_matching_microcode(int cpu, void *mc, int rev)
return 1; return 1;
} }
static void apply_microcode_amd(int cpu) static int apply_microcode_amd(int cpu)
{ {
unsigned long flags;
u32 rev, dummy; u32 rev, dummy;
int cpu_num = raw_smp_processor_id(); int cpu_num = raw_smp_processor_id();
struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num; struct ucode_cpu_info *uci = ucode_cpu_info + cpu_num;
...@@ -156,25 +140,25 @@ static void apply_microcode_amd(int cpu) ...@@ -156,25 +140,25 @@ static void apply_microcode_amd(int cpu)
BUG_ON(cpu_num != cpu); BUG_ON(cpu_num != cpu);
if (mc_amd == NULL) if (mc_amd == NULL)
return; return 0;
spin_lock_irqsave(&microcode_update_lock, flags);
wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code); wrmsrl(MSR_AMD64_PATCH_LOADER, (u64)(long)&mc_amd->hdr.data_code);
/* get patch id after patching */ /* get patch id after patching */
rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy);
spin_unlock_irqrestore(&microcode_update_lock, flags);
/* check current patch id and patch's id for match */ /* check current patch id and patch's id for match */
if (rev != mc_amd->hdr.patch_id) { if (rev != mc_amd->hdr.patch_id) {
printk(KERN_ERR "microcode: CPU%d: update failed " printk(KERN_ERR "microcode: CPU%d: update failed "
"(for patch_level=0x%x)\n", cpu, mc_amd->hdr.patch_id); "(for patch_level=0x%x)\n", cpu, mc_amd->hdr.patch_id);
return; return -1;
} }
printk(KERN_INFO "microcode: CPU%d: updated (new patch_level=0x%x)\n", printk(KERN_INFO "microcode: CPU%d: updated (new patch_level=0x%x)\n",
cpu, rev); cpu, rev);
uci->cpu_sig.rev = rev; uci->cpu_sig.rev = rev;
return 0;
} }
static int get_ucode_data(void *to, const u8 *from, size_t n) static int get_ucode_data(void *to, const u8 *from, size_t n)
...@@ -263,7 +247,8 @@ static void free_equiv_cpu_table(void) ...@@ -263,7 +247,8 @@ static void free_equiv_cpu_table(void)
} }
} }
static int generic_load_microcode(int cpu, const u8 *data, size_t size) static enum ucode_state
generic_load_microcode(int cpu, const u8 *data, size_t size)
{ {
struct ucode_cpu_info *uci = ucode_cpu_info + cpu; struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
const u8 *ucode_ptr = data; const u8 *ucode_ptr = data;
...@@ -272,12 +257,13 @@ static int generic_load_microcode(int cpu, const u8 *data, size_t size) ...@@ -272,12 +257,13 @@ static int generic_load_microcode(int cpu, const u8 *data, size_t size)
int new_rev = uci->cpu_sig.rev; int new_rev = uci->cpu_sig.rev;
unsigned int leftover; unsigned int leftover;
unsigned long offset; unsigned long offset;
enum ucode_state state = UCODE_OK;
offset = install_equiv_cpu_table(ucode_ptr); offset = install_equiv_cpu_table(ucode_ptr);
if (!offset) { if (!offset) {
printk(KERN_ERR "microcode: failed to create " printk(KERN_ERR "microcode: failed to create "
"equivalent cpu table\n"); "equivalent cpu table\n");
return -EINVAL; return UCODE_ERROR;
} }
ucode_ptr += offset; ucode_ptr += offset;
...@@ -312,28 +298,27 @@ static int generic_load_microcode(int cpu, const u8 *data, size_t size) ...@@ -312,28 +298,27 @@ static int generic_load_microcode(int cpu, const u8 *data, size_t size)
pr_debug("microcode: CPU%d found a matching microcode " pr_debug("microcode: CPU%d found a matching microcode "
"update with version 0x%x (current=0x%x)\n", "update with version 0x%x (current=0x%x)\n",
cpu, new_rev, uci->cpu_sig.rev); cpu, new_rev, uci->cpu_sig.rev);
} else } else {
vfree(new_mc); vfree(new_mc);
state = UCODE_ERROR;
} }
} else
state = UCODE_NFOUND;
free_equiv_cpu_table(); free_equiv_cpu_table();
return (int)leftover; return state;
} }
static int request_microcode_fw(int cpu, struct device *device) static enum ucode_state request_microcode_fw(int cpu, struct device *device)
{ {
const char *fw_name = "amd-ucode/microcode_amd.bin"; const char *fw_name = "amd-ucode/microcode_amd.bin";
const struct firmware *firmware; const struct firmware *firmware;
int ret; enum ucode_state ret;
/* We should bind the task to the CPU */
BUG_ON(cpu != raw_smp_processor_id());
ret = request_firmware(&firmware, fw_name, device); if (request_firmware(&firmware, fw_name, device)) {
if (ret) {
printk(KERN_ERR "microcode: failed to load file %s\n", fw_name); printk(KERN_ERR "microcode: failed to load file %s\n", fw_name);
return ret; return UCODE_NFOUND;
} }
ret = generic_load_microcode(cpu, firmware->data, firmware->size); ret = generic_load_microcode(cpu, firmware->data, firmware->size);
...@@ -343,11 +328,12 @@ static int request_microcode_fw(int cpu, struct device *device) ...@@ -343,11 +328,12 @@ static int request_microcode_fw(int cpu, struct device *device)
return ret; return ret;
} }
static int request_microcode_user(int cpu, const void __user *buf, size_t size) static enum ucode_state
request_microcode_user(int cpu, const void __user *buf, size_t size)
{ {
printk(KERN_INFO "microcode: AMD microcode update via " printk(KERN_INFO "microcode: AMD microcode update via "
"/dev/cpu/microcode not supported\n"); "/dev/cpu/microcode not supported\n");
return -1; return UCODE_ERROR;
} }
static void microcode_fini_cpu_amd(int cpu) static void microcode_fini_cpu_amd(int cpu)
......
This diff is collapsed.
...@@ -70,24 +70,11 @@ ...@@ -70,24 +70,11 @@
* Fix sigmatch() macro to handle old CPUs with pf == 0. * Fix sigmatch() macro to handle old CPUs with pf == 0.
* Thanks to Stuart Swales for pointing out this bug. * Thanks to Stuart Swales for pointing out this bug.
*/ */
#include <linux/platform_device.h>
#include <linux/capability.h>
#include <linux/miscdevice.h>
#include <linux/firmware.h> #include <linux/firmware.h>
#include <linux/smp_lock.h>
#include <linux/spinlock.h>
#include <linux/cpumask.h>
#include <linux/uaccess.h> #include <linux/uaccess.h>
#include <linux/vmalloc.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/mutex.h> #include <linux/vmalloc.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/cpu.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <asm/microcode.h> #include <asm/microcode.h>
#include <asm/processor.h> #include <asm/processor.h>
...@@ -150,13 +137,9 @@ struct extended_sigtable { ...@@ -150,13 +137,9 @@ struct extended_sigtable {
#define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE) #define exttable_size(et) ((et)->count * EXT_SIGNATURE_SIZE + EXT_HEADER_SIZE)
/* serialize access to the physical write to MSR 0x79 */
static DEFINE_SPINLOCK(microcode_update_lock);
static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
{ {
struct cpuinfo_x86 *c = &cpu_data(cpu_num); struct cpuinfo_x86 *c = &cpu_data(cpu_num);
unsigned long flags;
unsigned int val[2]; unsigned int val[2];
memset(csig, 0, sizeof(*csig)); memset(csig, 0, sizeof(*csig));
...@@ -176,18 +159,14 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig) ...@@ -176,18 +159,14 @@ static int collect_cpu_info(int cpu_num, struct cpu_signature *csig)
csig->pf = 1 << ((val[1] >> 18) & 7); csig->pf = 1 << ((val[1] >> 18) & 7);
} }
/* serialize access to the physical write to MSR 0x79 */
spin_lock_irqsave(&microcode_update_lock, flags);
wrmsr(MSR_IA32_UCODE_REV, 0, 0); wrmsr(MSR_IA32_UCODE_REV, 0, 0);
/* see notes above for revision 1.07. Apparent chip bug */ /* see notes above for revision 1.07. Apparent chip bug */
sync_core(); sync_core();
/* get the current revision from MSR 0x8B */ /* get the current revision from MSR 0x8B */
rdmsr(MSR_IA32_UCODE_REV, val[0], csig->rev); rdmsr(MSR_IA32_UCODE_REV, val[0], csig->rev);
spin_unlock_irqrestore(&microcode_update_lock, flags);
pr_debug("microcode: collect_cpu_info : sig=0x%x, pf=0x%x, rev=0x%x\n", printk(KERN_INFO "microcode: CPU%d sig=0x%x, pf=0x%x, revision=0x%x\n",
csig->sig, csig->pf, csig->rev); cpu_num, csig->sig, csig->pf, csig->rev);
return 0; return 0;
} }
...@@ -318,11 +297,10 @@ get_matching_microcode(struct cpu_signature *cpu_sig, void *mc, int rev) ...@@ -318,11 +297,10 @@ get_matching_microcode(struct cpu_signature *cpu_sig, void *mc, int rev)
return 0; return 0;
} }
static void apply_microcode(int cpu) static int apply_microcode(int cpu)
{ {
struct microcode_intel *mc_intel; struct microcode_intel *mc_intel;
struct ucode_cpu_info *uci; struct ucode_cpu_info *uci;
unsigned long flags;
unsigned int val[2]; unsigned int val[2];
int cpu_num; int cpu_num;
...@@ -334,10 +312,7 @@ static void apply_microcode(int cpu) ...@@ -334,10 +312,7 @@ static void apply_microcode(int cpu)
BUG_ON(cpu_num != cpu); BUG_ON(cpu_num != cpu);
if (mc_intel == NULL) if (mc_intel == NULL)
return; return 0;
/* serialize access to the physical write to MSR 0x79 */
spin_lock_irqsave(&microcode_update_lock, flags);
/* write microcode via MSR 0x79 */ /* write microcode via MSR 0x79 */
wrmsr(MSR_IA32_UCODE_WRITE, wrmsr(MSR_IA32_UCODE_WRITE,
...@@ -351,30 +326,32 @@ static void apply_microcode(int cpu) ...@@ -351,30 +326,32 @@ static void apply_microcode(int cpu)
/* get the current revision from MSR 0x8B */ /* get the current revision from MSR 0x8B */
rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]); rdmsr(MSR_IA32_UCODE_REV, val[0], val[1]);
spin_unlock_irqrestore(&microcode_update_lock, flags);
if (val[1] != mc_intel->hdr.rev) { if (val[1] != mc_intel->hdr.rev) {
printk(KERN_ERR "microcode: CPU%d update from revision " printk(KERN_ERR "microcode: CPU%d update "
"0x%x to 0x%x failed\n", "to revision 0x%x failed\n",
cpu_num, uci->cpu_sig.rev, val[1]); cpu_num, mc_intel->hdr.rev);
return; return -1;
} }
printk(KERN_INFO "microcode: CPU%d updated from revision " printk(KERN_INFO "microcode: CPU%d updated to revision "
"0x%x to 0x%x, date = %04x-%02x-%02x \n", "0x%x, date = %04x-%02x-%02x \n",
cpu_num, uci->cpu_sig.rev, val[1], cpu_num, val[1],
mc_intel->hdr.date & 0xffff, mc_intel->hdr.date & 0xffff,
mc_intel->hdr.date >> 24, mc_intel->hdr.date >> 24,
(mc_intel->hdr.date >> 16) & 0xff); (mc_intel->hdr.date >> 16) & 0xff);
uci->cpu_sig.rev = val[1]; uci->cpu_sig.rev = val[1];
return 0;
} }
static int generic_load_microcode(int cpu, void *data, size_t size, static enum ucode_state generic_load_microcode(int cpu, void *data, size_t size,
int (*get_ucode_data)(void *, const void *, size_t)) int (*get_ucode_data)(void *, const void *, size_t))
{ {
struct ucode_cpu_info *uci = ucode_cpu_info + cpu; struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
u8 *ucode_ptr = data, *new_mc = NULL, *mc; u8 *ucode_ptr = data, *new_mc = NULL, *mc;
int new_rev = uci->cpu_sig.rev; int new_rev = uci->cpu_sig.rev;
unsigned int leftover = size; unsigned int leftover = size;
enum ucode_state state = UCODE_OK;
while (leftover) { while (leftover) {
struct microcode_header_intel mc_header; struct microcode_header_intel mc_header;
...@@ -412,11 +389,15 @@ static int generic_load_microcode(int cpu, void *data, size_t size, ...@@ -412,11 +389,15 @@ static int generic_load_microcode(int cpu, void *data, size_t size,
leftover -= mc_size; leftover -= mc_size;
} }
if (!new_mc)
goto out;
if (leftover) { if (leftover) {
if (new_mc)
vfree(new_mc); vfree(new_mc);
state = UCODE_ERROR;
goto out;
}
if (!new_mc) {
state = UCODE_NFOUND;
goto out; goto out;
} }
...@@ -427,9 +408,8 @@ static int generic_load_microcode(int cpu, void *data, size_t size, ...@@ -427,9 +408,8 @@ static int generic_load_microcode(int cpu, void *data, size_t size,
pr_debug("microcode: CPU%d found a matching microcode update with" pr_debug("microcode: CPU%d found a matching microcode update with"
" version 0x%x (current=0x%x)\n", " version 0x%x (current=0x%x)\n",
cpu, new_rev, uci->cpu_sig.rev); cpu, new_rev, uci->cpu_sig.rev);
out:
out: return state;
return (int)leftover;
} }
static int get_ucode_fw(void *to, const void *from, size_t n) static int get_ucode_fw(void *to, const void *from, size_t n)
...@@ -438,21 +418,19 @@ static int get_ucode_fw(void *to, const void *from, size_t n) ...@@ -438,21 +418,19 @@ static int get_ucode_fw(void *to, const void *from, size_t n)
return 0; return 0;
} }
static int request_microcode_fw(int cpu, struct device *device) static enum ucode_state request_microcode_fw(int cpu, struct device *device)
{ {
char name[30]; char name[30];
struct cpuinfo_x86 *c = &cpu_data(cpu); struct cpuinfo_x86 *c = &cpu_data(cpu);
const struct firmware *firmware; const struct firmware *firmware;
int ret; enum ucode_state ret;
/* We should bind the task to the CPU */
BUG_ON(cpu != raw_smp_processor_id());
sprintf(name, "intel-ucode/%02x-%02x-%02x", sprintf(name, "intel-ucode/%02x-%02x-%02x",
c->x86, c->x86_model, c->x86_mask); c->x86, c->x86_model, c->x86_mask);
ret = request_firmware(&firmware, name, device);
if (ret) { if (request_firmware(&firmware, name, device)) {
pr_debug("microcode: data file %s load failed\n", name); pr_debug("microcode: data file %s load failed\n", name);
return ret; return UCODE_NFOUND;
} }
ret = generic_load_microcode(cpu, (void *)firmware->data, ret = generic_load_microcode(cpu, (void *)firmware->data,
...@@ -468,11 +446,9 @@ static int get_ucode_user(void *to, const void *from, size_t n) ...@@ -468,11 +446,9 @@ static int get_ucode_user(void *to, const void *from, size_t n)
return copy_from_user(to, from, n); return copy_from_user(to, from, n);
} }
static int request_microcode_user(int cpu, const void __user *buf, size_t size) static enum ucode_state
request_microcode_user(int cpu, const void __user *buf, size_t size)
{ {
/* We should bind the task to the CPU */
BUG_ON(cpu != raw_smp_processor_id());
return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user); return generic_load_microcode(cpu, (void *)buf, size, &get_ucode_user);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment