Skip to content

Commit 3a5dc1f

Browse files
committed
Merge branch 'x86-microcode-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull x86 microcode loading updates from Ingo Molnar: "The main changes in this cycle are: - Reload microcode when resuming and the case when only the early loader has been utilized. (Borislav Petkov) - Also, do not load the driver on paravirt guests. (Boris Ostrovsky)" * 'x86-microcode-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: x86/microcode/intel: Fish out the stashed microcode for the BSP x86, microcode: Reload microcode on resume x86, microcode: Don't initialize microcode code on paravirt x86, microcode, intel: Drop unused parameter x86, microcode, AMD: Do not use smp_processor_id() in preemtible context
2 parents 3100e44 + 25cdb9c commit 3a5dc1f

8 files changed

Lines changed: 87 additions & 24 deletions

File tree

‎arch/x86/include/asm/microcode.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ static inline void __exit exit_amd_microcode(void) {}
7878
extern void __init load_ucode_bsp(void);
7979
extern void load_ucode_ap(void);
8080
extern int __init save_microcode_in_initrd(void);
81+
void reload_early_microcode(void);
8182
#else
8283
static inline void __init load_ucode_bsp(void) {}
8384
static inline void load_ucode_ap(void) {}
8485
static inline int __init save_microcode_in_initrd(void)
8586
{
8687
return 0;
8788
}
89+
static inline void reload_early_microcode(void) {}
8890
#endif
8991

9092
#endif /* _ASM_X86_MICROCODE_H */

‎arch/x86/include/asm/microcode_amd.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static inline u16 find_equiv_id(struct equiv_cpu_entry *equiv_cpu_table,
5959

6060
extern int __apply_microcode_amd(struct microcode_amd *mc_amd);
6161
extern int apply_microcode_amd(int cpu);
62-
extern enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size);
62+
extern enum ucode_state load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size);
6363

6464
#define PATCH_MAX_SIZE PAGE_SIZE
6565
extern u8 amd_ucode_patch[PATCH_MAX_SIZE];
@@ -68,10 +68,12 @@ extern u8 amd_ucode_patch[PATCH_MAX_SIZE];
6868
extern void __init load_ucode_amd_bsp(void);
6969
extern void load_ucode_amd_ap(void);
7070
extern int __init save_microcode_in_initrd_amd(void);
71+
void reload_ucode_amd(void);
7172
#else
7273
static inline void __init load_ucode_amd_bsp(void) {}
7374
static inline void load_ucode_amd_ap(void) {}
7475
static inline int __init save_microcode_in_initrd_amd(void) { return -EINVAL; }
76+
void reload_ucode_amd(void) {}
7577
#endif
7678

7779
#endif /* _ASM_X86_MICROCODE_AMD_H */

‎arch/x86/include/asm/microcode_intel.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,13 @@ extern void __init load_ucode_intel_bsp(void);
6868
extern void load_ucode_intel_ap(void);
6969
extern void show_ucode_info_early(void);
7070
extern int __init save_microcode_in_initrd_intel(void);
71+
void reload_ucode_intel(void);
7172
#else
7273
static inline __init void load_ucode_intel_bsp(void) {}
7374
static inline void load_ucode_intel_ap(void) {}
7475
static inline void show_ucode_info_early(void) {}
7576
static inline int __init save_microcode_in_initrd_intel(void) { return -EINVAL; }
77+
static inline void reload_ucode_intel(void) {}
7678
#endif
7779

7880
#if defined(CONFIG_MICROCODE_INTEL_EARLY) && defined(CONFIG_HOTPLUG_CPU)

‎arch/x86/kernel/cpu/microcode/amd.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ static enum ucode_state __load_microcode_amd(u8 family, const u8 *data,
376376
return UCODE_OK;
377377
}
378378

379-
enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
379+
enum ucode_state load_microcode_amd(int cpu, u8 family, const u8 *data, size_t size)
380380
{
381381
enum ucode_state ret;
382382

@@ -390,8 +390,8 @@ enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size)
390390

391391
#if defined(CONFIG_MICROCODE_AMD_EARLY) && defined(CONFIG_X86_32)
392392
/* save BSP's matching patch for early load */
393-
if (cpu_data(smp_processor_id()).cpu_index == boot_cpu_data.cpu_index) {
394-
struct ucode_patch *p = find_patch(smp_processor_id());
393+
if (cpu_data(cpu).cpu_index == boot_cpu_data.cpu_index) {
394+
struct ucode_patch *p = find_patch(cpu);
395395
if (p) {
396396
memset(amd_ucode_patch, 0, PATCH_MAX_SIZE);
397397
memcpy(amd_ucode_patch, p->data, min_t(u32, ksize(p->data),
@@ -444,7 +444,7 @@ static enum ucode_state request_microcode_amd(int cpu, struct device *device,
444444
goto fw_release;
445445
}
446446

447-
ret = load_microcode_amd(c->x86, fw->data, fw->size);
447+
ret = load_microcode_amd(cpu, c->x86, fw->data, fw->size);
448448

449449
fw_release:
450450
release_firmware(fw);

‎arch/x86/kernel/cpu/microcode/amd_early.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ int __init save_microcode_in_initrd_amd(void)
389389
eax = cpuid_eax(0x00000001);
390390
eax = ((eax >> 8) & 0xf) + ((eax >> 20) & 0xff);
391391

392-
ret = load_microcode_amd(eax, container, container_size);
392+
ret = load_microcode_amd(smp_processor_id(), eax, container, container_size);
393393
if (ret != UCODE_OK)
394394
retval = -EINVAL;
395395

@@ -402,3 +402,21 @@ int __init save_microcode_in_initrd_amd(void)
402402

403403
return retval;
404404
}
405+
406+
void reload_ucode_amd(void)
407+
{
408+
struct microcode_amd *mc;
409+
u32 rev, eax;
410+
411+
rdmsr(MSR_AMD64_PATCH_LEVEL, rev, eax);
412+
413+
mc = (struct microcode_amd *)amd_ucode_patch;
414+
415+
if (mc && rev < mc->hdr.patch_id) {
416+
if (!__apply_microcode_amd(mc)) {
417+
ucode_new_rev = mc->hdr.patch_id;
418+
pr_info("microcode: reload patch_level=0x%08x\n",
419+
ucode_new_rev);
420+
}
421+
}
422+
}

‎arch/x86/kernel/cpu/microcode/core.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,8 @@ static void mc_bp_resume(void)
465465

466466
if (uci->valid && uci->mc)
467467
microcode_ops->apply_microcode(cpu);
468-
#ifdef CONFIG_X86_64
469468
else if (!uci->mc)
470-
/*
471-
* We might resume and not have applied late microcode but still
472-
* have a newer patch stashed from the early loader. We don't
473-
* have it in uci->mc so we have to load it the same way we're
474-
* applying patches early on the APs.
475-
*/
476-
load_ucode_ap();
477-
#endif
469+
reload_early_microcode();
478470
}
479471

480472
static struct syscore_ops mc_syscore_ops = {
@@ -559,7 +551,7 @@ static int __init microcode_init(void)
559551
struct cpuinfo_x86 *c = &cpu_data(0);
560552
int error;
561553

562-
if (dis_ucode_ldr)
554+
if (paravirt_enabled() || dis_ucode_ldr)
563555
return 0;
564556

565557
if (c->x86_vendor == X86_VENDOR_INTEL)

‎arch/x86/kernel/cpu/microcode/core_early.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,24 @@ int __init save_microcode_in_initrd(void)
176176

177177
return 0;
178178
}
179+
180+
void reload_early_microcode(void)
181+
{
182+
int vendor, x86;
183+
184+
vendor = x86_vendor();
185+
x86 = x86_family();
186+
187+
switch (vendor) {
188+
case X86_VENDOR_INTEL:
189+
if (x86 >= 6)
190+
reload_ucode_intel();
191+
break;
192+
case X86_VENDOR_AMD:
193+
if (x86 >= 0x10)
194+
reload_ucode_amd();
195+
break;
196+
default:
197+
break;
198+
}
199+
}

‎arch/x86/kernel/cpu/microcode/intel_early.c‎

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,7 @@ static inline void print_ucode(struct ucode_cpu_info *uci)
650650
}
651651
#endif
652652

653-
static int apply_microcode_early(struct mc_saved_data *mc_saved_data,
654-
struct ucode_cpu_info *uci)
653+
static int apply_microcode_early(struct ucode_cpu_info *uci, bool early)
655654
{
656655
struct microcode_intel *mc_intel;
657656
unsigned int val[2];
@@ -680,7 +679,10 @@ static int apply_microcode_early(struct mc_saved_data *mc_saved_data,
680679
#endif
681680
uci->cpu_sig.rev = val[1];
682681

683-
print_ucode(uci);
682+
if (early)
683+
print_ucode(uci);
684+
else
685+
print_ucode_info(uci, mc_intel->hdr.date);
684686

685687
return 0;
686688
}
@@ -715,12 +717,17 @@ _load_ucode_intel_bsp(struct mc_saved_data *mc_saved_data,
715717
unsigned long initrd_end_early,
716718
struct ucode_cpu_info *uci)
717719
{
720+
enum ucode_state ret;
721+
718722
collect_cpu_info_early(uci);
719723
scan_microcode(initrd_start_early, initrd_end_early, mc_saved_data,
720724
mc_saved_in_initrd, uci);
721-
load_microcode(mc_saved_data, mc_saved_in_initrd,
722-
initrd_start_early, uci);
723-
apply_microcode_early(mc_saved_data, uci);
725+
726+
ret = load_microcode(mc_saved_data, mc_saved_in_initrd,
727+
initrd_start_early, uci);
728+
729+
if (ret == UCODE_OK)
730+
apply_microcode_early(uci, true);
724731
}
725732

726733
void __init
@@ -749,7 +756,8 @@ load_ucode_intel_bsp(void)
749756
initrd_end_early = initrd_start_early + ramdisk_size;
750757

751758
_load_ucode_intel_bsp(&mc_saved_data, mc_saved_in_initrd,
752-
initrd_start_early, initrd_end_early, &uci);
759+
initrd_start_early, initrd_end_early,
760+
&uci);
753761
#endif
754762
}
755763

@@ -783,5 +791,23 @@ void load_ucode_intel_ap(void)
783791
collect_cpu_info_early(&uci);
784792
load_microcode(mc_saved_data_p, mc_saved_in_initrd_p,
785793
initrd_start_addr, &uci);
786-
apply_microcode_early(mc_saved_data_p, &uci);
794+
apply_microcode_early(&uci, true);
795+
}
796+
797+
void reload_ucode_intel(void)
798+
{
799+
struct ucode_cpu_info uci;
800+
enum ucode_state ret;
801+
802+
if (!mc_saved_data.mc_saved_count)
803+
return;
804+
805+
collect_cpu_info_early(&uci);
806+
807+
ret = generic_load_microcode_early(mc_saved_data.mc_saved,
808+
mc_saved_data.mc_saved_count, &uci);
809+
if (ret != UCODE_OK)
810+
return;
811+
812+
apply_microcode_early(&uci, false);
787813
}

0 commit comments

Comments
 (0)