Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'x86_microcode_for_v6.3_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 microcode loader updates from Borislav Petkov:

- Fix mixed steppings support on AMD which got broken somewhere along
the way

- Improve revision reporting

- Properly check CPUID capabilities after late microcode upgrade to
avoid false positives

- A garden variety of other small fixes

* tag 'x86_microcode_for_v6.3_rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
x86/microcode/core: Return an error only when necessary
x86/microcode/AMD: Fix mixed steppings support
x86/microcode/AMD: Add a @cpu parameter to the reloading functions
x86/microcode/amd: Remove load_microcode_amd()'s bsp parameter
x86/microcode: Allow only "1" as a late reload trigger value
x86/microcode/intel: Print old and new revision during early boot
x86/microcode/intel: Pass the microcode revision to print_ucode_info() directly
x86/microcode: Adjust late loading result reporting message
x86/microcode: Check CPU capabilities after late microcode update correctly
x86/microcode: Add a parameter to microcode_check() to store CPU capabilities
x86/microcode: Use the DEVICE_ATTR_RO() macro
x86/microcode/AMD: Handle multiple glued containers properly
x86/microcode/AMD: Rename a couple of functions

+117 -104
+2 -2
arch/x86/include/asm/microcode.h
··· 125 125 #ifdef CONFIG_MICROCODE 126 126 extern void __init load_ucode_bsp(void); 127 127 extern void load_ucode_ap(void); 128 - void reload_early_microcode(void); 128 + void reload_early_microcode(unsigned int cpu); 129 129 extern bool initrd_gone; 130 130 void microcode_bsp_resume(void); 131 131 #else 132 132 static inline void __init load_ucode_bsp(void) { } 133 133 static inline void load_ucode_ap(void) { } 134 - static inline void reload_early_microcode(void) { } 134 + static inline void reload_early_microcode(unsigned int cpu) { } 135 135 static inline void microcode_bsp_resume(void) { } 136 136 #endif 137 137
+2 -2
arch/x86/include/asm/microcode_amd.h
··· 47 47 extern void __init load_ucode_amd_bsp(unsigned int family); 48 48 extern void load_ucode_amd_ap(unsigned int family); 49 49 extern int __init save_microcode_in_initrd_amd(unsigned int family); 50 - void reload_ucode_amd(void); 50 + void reload_ucode_amd(unsigned int cpu); 51 51 #else 52 52 static inline void __init load_ucode_amd_bsp(unsigned int family) {} 53 53 static inline void load_ucode_amd_ap(unsigned int family) {} 54 54 static inline int __init 55 55 save_microcode_in_initrd_amd(unsigned int family) { return -EINVAL; } 56 - static inline void reload_ucode_amd(void) {} 56 + static inline void reload_ucode_amd(unsigned int cpu) {} 57 57 #endif 58 58 #endif /* _ASM_X86_MICROCODE_AMD_H */
+2 -1
arch/x86/include/asm/processor.h
··· 697 697 #endif 698 698 699 699 void __noreturn stop_this_cpu(void *dummy); 700 - void microcode_check(void); 700 + void microcode_check(struct cpuinfo_x86 *prev_info); 701 + void store_cpu_caps(struct cpuinfo_x86 *info); 701 702 702 703 enum l1tf_mitigations { 703 704 L1TF_MITIGATION_OFF,
+30 -15
arch/x86/kernel/cpu/common.c
··· 2302 2302 #endif 2303 2303 2304 2304 #ifdef CONFIG_MICROCODE_LATE_LOADING 2305 - /* 2305 + /** 2306 + * store_cpu_caps() - Store a snapshot of CPU capabilities 2307 + * @curr_info: Pointer where to store it 2308 + * 2309 + * Returns: None 2310 + */ 2311 + void store_cpu_caps(struct cpuinfo_x86 *curr_info) 2312 + { 2313 + /* Reload CPUID max function as it might've changed. */ 2314 + curr_info->cpuid_level = cpuid_eax(0); 2315 + 2316 + /* Copy all capability leafs and pick up the synthetic ones. */ 2317 + memcpy(&curr_info->x86_capability, &boot_cpu_data.x86_capability, 2318 + sizeof(curr_info->x86_capability)); 2319 + 2320 + /* Get the hardware CPUID leafs */ 2321 + get_cpu_cap(curr_info); 2322 + } 2323 + 2324 + /** 2325 + * microcode_check() - Check if any CPU capabilities changed after an update. 2326 + * @prev_info: CPU capabilities stored before an update. 2327 + * 2306 2328 * The microcode loader calls this upon late microcode load to recheck features, 2307 2329 * only when microcode has been updated. Caller holds microcode_mutex and CPU 2308 2330 * hotplug lock. 2331 + * 2332 + * Return: None 2309 2333 */ 2310 - void microcode_check(void) 2334 + void microcode_check(struct cpuinfo_x86 *prev_info) 2311 2335 { 2312 - struct cpuinfo_x86 info; 2336 + struct cpuinfo_x86 curr_info; 2313 2337 2314 2338 perf_check_microcode(); 2315 2339 2316 - /* Reload CPUID max function as it might've changed. */ 2317 - info.cpuid_level = cpuid_eax(0); 2340 + store_cpu_caps(&curr_info); 2318 2341 2319 - /* 2320 - * Copy all capability leafs to pick up the synthetic ones so that 2321 - * memcmp() below doesn't fail on that. The ones coming from CPUID will 2322 - * get overwritten in get_cpu_cap(). 2323 - */ 2324 - memcpy(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability)); 2325 - 2326 - get_cpu_cap(&info); 2327 - 2328 - if (!memcmp(&info.x86_capability, &boot_cpu_data.x86_capability, sizeof(info.x86_capability))) 2342 + if (!memcmp(&prev_info->x86_capability, &curr_info.x86_capability, 2343 + sizeof(prev_info->x86_capability))) 2329 2344 return; 2330 2345 2331 2346 pr_warn("x86/CPU: CPU features have changed after loading microcode, but might not take effect.\n");
+37 -39
arch/x86/kernel/cpu/microcode/amd.c
··· 55 55 }; 56 56 57 57 static u32 ucode_new_rev; 58 - static u8 amd_ucode_patch[PATCH_MAX_SIZE]; 58 + 59 + /* One blob per node. */ 60 + static u8 amd_ucode_patch[MAX_NUMNODES][PATCH_MAX_SIZE]; 59 61 60 62 /* 61 63 * Microcode patch container file is prepended to the initrd in cpio ··· 332 330 ret = verify_patch(x86_family(desc->cpuid_1_eax), buf, size, &patch_size, true); 333 331 if (ret < 0) { 334 332 /* 335 - * Patch verification failed, skip to the next 336 - * container, if there's one: 333 + * Patch verification failed, skip to the next container, if 334 + * there is one. Before exit, check whether that container has 335 + * found a patch already. If so, use it. 337 336 */ 338 337 goto out; 339 338 } else if (ret > 0) { ··· 353 350 size -= patch_size + SECTION_HDR_SIZE; 354 351 } 355 352 353 + out: 356 354 /* 357 355 * If we have found a patch (desc->mc), it means we're looking at the 358 356 * container which has a patch for this CPU so return 0 to mean, @ucode ··· 368 364 return 0; 369 365 } 370 366 371 - out: 372 367 return orig_size - size; 373 368 } 374 369 ··· 417 414 * 418 415 * Returns true if container found (sets @desc), false otherwise. 419 416 */ 420 - static bool 421 - apply_microcode_early_amd(u32 cpuid_1_eax, void *ucode, size_t size, bool save_patch) 417 + static bool early_apply_microcode(u32 cpuid_1_eax, void *ucode, size_t size, bool save_patch) 422 418 { 423 419 struct cont_desc desc = { 0 }; 424 420 u8 (*patch)[PATCH_MAX_SIZE]; ··· 430 428 patch = (u8 (*)[PATCH_MAX_SIZE])__pa_nodebug(&amd_ucode_patch); 431 429 #else 432 430 new_rev = &ucode_new_rev; 433 - patch = &amd_ucode_patch; 431 + patch = &amd_ucode_patch[0]; 434 432 #endif 435 433 436 434 desc.cpuid_1_eax = cpuid_1_eax; ··· 483 481 return false; 484 482 } 485 483 486 - static void __load_ucode_amd(unsigned int cpuid_1_eax, struct cpio_data *ret) 484 + static void find_blobs_in_containers(unsigned int cpuid_1_eax, struct cpio_data *ret) 487 485 { 488 486 struct ucode_cpu_info *uci; 489 487 struct cpio_data cp; ··· 513 511 { 514 512 struct cpio_data cp = { }; 515 513 516 - __load_ucode_amd(cpuid_1_eax, &cp); 514 + find_blobs_in_containers(cpuid_1_eax, &cp); 517 515 if (!(cp.data && cp.size)) 518 516 return; 519 517 520 - apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, true); 518 + early_apply_microcode(cpuid_1_eax, cp.data, cp.size, true); 521 519 } 522 520 523 521 void load_ucode_amd_ap(unsigned int cpuid_1_eax) ··· 548 546 } 549 547 } 550 548 551 - __load_ucode_amd(cpuid_1_eax, &cp); 549 + find_blobs_in_containers(cpuid_1_eax, &cp); 552 550 if (!(cp.data && cp.size)) 553 551 return; 554 552 555 - apply_microcode_early_amd(cpuid_1_eax, cp.data, cp.size, false); 553 + early_apply_microcode(cpuid_1_eax, cp.data, cp.size, false); 556 554 } 557 555 558 - static enum ucode_state 559 - load_microcode_amd(bool save, u8 family, const u8 *data, size_t size); 556 + static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size); 560 557 561 558 int __init save_microcode_in_initrd_amd(unsigned int cpuid_1_eax) 562 559 { ··· 573 572 if (!desc.mc) 574 573 return -EINVAL; 575 574 576 - ret = load_microcode_amd(true, x86_family(cpuid_1_eax), desc.data, desc.size); 575 + ret = load_microcode_amd(x86_family(cpuid_1_eax), desc.data, desc.size); 577 576 if (ret > UCODE_UPDATED) 578 577 return -EINVAL; 579 578 580 579 return 0; 581 580 } 582 581 583 - void reload_ucode_amd(void) 582 + void reload_ucode_amd(unsigned int cpu) 584 583 { 585 - struct microcode_amd *mc; 586 584 u32 rev, dummy __always_unused; 585 + struct microcode_amd *mc; 587 586 588 - mc = (struct microcode_amd *)amd_ucode_patch; 587 + mc = (struct microcode_amd *)amd_ucode_patch[cpu_to_node(cpu)]; 589 588 590 589 rdmsr(MSR_AMD64_PATCH_LEVEL, rev, dummy); 591 590 ··· 817 816 return 0; 818 817 } 819 818 819 + /* Scan the blob in @data and add microcode patches to the cache. */ 820 820 static enum ucode_state __load_microcode_amd(u8 family, const u8 *data, 821 821 size_t size) 822 822 { ··· 852 850 return UCODE_OK; 853 851 } 854 852 855 - static enum ucode_state 856 - load_microcode_amd(bool save, u8 family, const u8 *data, size_t size) 853 + static enum ucode_state load_microcode_amd(u8 family, const u8 *data, size_t size) 857 854 { 855 + struct cpuinfo_x86 *c; 856 + unsigned int nid, cpu; 858 857 struct ucode_patch *p; 859 858 enum ucode_state ret; 860 859 ··· 868 865 return ret; 869 866 } 870 867 871 - p = find_patch(0); 872 - if (!p) { 873 - return ret; 874 - } else { 875 - if (boot_cpu_data.microcode >= p->patch_id) 876 - return ret; 868 + for_each_node(nid) { 869 + cpu = cpumask_first(cpumask_of_node(nid)); 870 + c = &cpu_data(cpu); 871 + 872 + p = find_patch(cpu); 873 + if (!p) 874 + continue; 875 + 876 + if (c->microcode >= p->patch_id) 877 + continue; 877 878 878 879 ret = UCODE_NEW; 880 + 881 + memset(&amd_ucode_patch[nid], 0, PATCH_MAX_SIZE); 882 + memcpy(&amd_ucode_patch[nid], p->data, min_t(u32, p->size, PATCH_MAX_SIZE)); 879 883 } 880 - 881 - /* save BSP's matching patch for early load */ 882 - if (!save) 883 - return ret; 884 - 885 - memset(amd_ucode_patch, 0, PATCH_MAX_SIZE); 886 - memcpy(amd_ucode_patch, p->data, min_t(u32, p->size, PATCH_MAX_SIZE)); 887 884 888 885 return ret; 889 886 } ··· 908 905 { 909 906 char fw_name[36] = "amd-ucode/microcode_amd.bin"; 910 907 struct cpuinfo_x86 *c = &cpu_data(cpu); 911 - bool bsp = c->cpu_index == boot_cpu_data.cpu_index; 912 908 enum ucode_state ret = UCODE_NFOUND; 913 909 const struct firmware *fw; 914 - 915 - /* reload ucode container only on the boot cpu */ 916 - if (!bsp) 917 - return UCODE_OK; 918 910 919 911 if (c->x86 >= 0x15) 920 912 snprintf(fw_name, sizeof(fw_name), "amd-ucode/microcode_amd_fam%.2xh.bin", c->x86); ··· 923 925 if (!verify_container(fw->data, fw->size, false)) 924 926 goto fw_release; 925 927 926 - ret = load_microcode_amd(bsp, c->x86, fw->data, fw->size); 928 + ret = load_microcode_amd(c->x86, fw->data, fw->size); 927 929 928 930 fw_release: 929 931 release_firmware(fw);
+26 -19
arch/x86/kernel/cpu/microcode/core.c
··· 298 298 #endif 299 299 } 300 300 301 - void reload_early_microcode(void) 301 + void reload_early_microcode(unsigned int cpu) 302 302 { 303 303 int vendor, family; 304 304 ··· 312 312 break; 313 313 case X86_VENDOR_AMD: 314 314 if (family >= 0x10) 315 - reload_ucode_amd(); 315 + reload_ucode_amd(cpu); 316 316 break; 317 317 default: 318 318 break; ··· 409 409 goto wait_for_siblings; 410 410 411 411 if (err >= UCODE_NFOUND) { 412 - if (err == UCODE_ERROR) 412 + if (err == UCODE_ERROR) { 413 413 pr_warn("Error reloading microcode on CPU %d\n", cpu); 414 - 415 - ret = -1; 414 + ret = -1; 415 + } 416 416 } 417 417 418 418 wait_for_siblings: ··· 438 438 static int microcode_reload_late(void) 439 439 { 440 440 int old = boot_cpu_data.microcode, ret; 441 + struct cpuinfo_x86 prev_info; 441 442 442 443 pr_err("Attempting late microcode loading - it is dangerous and taints the kernel.\n"); 443 444 pr_err("You should switch to early loading, if possible.\n"); ··· 446 445 atomic_set(&late_cpus_in, 0); 447 446 atomic_set(&late_cpus_out, 0); 448 447 449 - ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); 450 - if (ret == 0) 451 - microcode_check(); 448 + /* 449 + * Take a snapshot before the microcode update in order to compare and 450 + * check whether any bits changed after an update. 451 + */ 452 + store_cpu_caps(&prev_info); 452 453 453 - pr_info("Reload completed, microcode revision: 0x%x -> 0x%x\n", 454 - old, boot_cpu_data.microcode); 454 + ret = stop_machine_cpuslocked(__reload_late, NULL, cpu_online_mask); 455 + if (!ret) { 456 + pr_info("Reload succeeded, microcode revision: 0x%x -> 0x%x\n", 457 + old, boot_cpu_data.microcode); 458 + microcode_check(&prev_info); 459 + } else { 460 + pr_info("Reload failed, current microcode revision: 0x%x\n", 461 + boot_cpu_data.microcode); 462 + } 455 463 456 464 return ret; 457 465 } ··· 475 465 ssize_t ret = 0; 476 466 477 467 ret = kstrtoul(buf, 0, &val); 478 - if (ret) 479 - return ret; 480 - 481 - if (val != 1) 482 - return size; 468 + if (ret || val != 1) 469 + return -EINVAL; 483 470 484 471 cpus_read_lock(); 485 472 ··· 514 507 return sprintf(buf, "0x%x\n", uci->cpu_sig.rev); 515 508 } 516 509 517 - static ssize_t pf_show(struct device *dev, 510 + static ssize_t processor_flags_show(struct device *dev, 518 511 struct device_attribute *attr, char *buf) 519 512 { 520 513 struct ucode_cpu_info *uci = ucode_cpu_info + dev->id; ··· 522 515 return sprintf(buf, "0x%x\n", uci->cpu_sig.pf); 523 516 } 524 517 525 - static DEVICE_ATTR(version, 0444, version_show, NULL); 526 - static DEVICE_ATTR(processor_flags, 0444, pf_show, NULL); 518 + static DEVICE_ATTR_RO(version); 519 + static DEVICE_ATTR_RO(processor_flags); 527 520 528 521 static struct attribute *mc_default_attrs[] = { 529 522 &dev_attr_version.attr, ··· 564 557 if (uci->mc) 565 558 microcode_ops->apply_microcode(cpu); 566 559 else 567 - reload_early_microcode(); 560 + reload_early_microcode(cpu); 568 561 } 569 562 570 563 static struct syscore_ops mc_syscore_ops = {
+18 -26
arch/x86/kernel/cpu/microcode/intel.c
··· 305 305 return false; 306 306 } 307 307 308 - /* 309 - * Print ucode update info. 310 - */ 311 - static void 312 - print_ucode_info(struct ucode_cpu_info *uci, unsigned int date) 308 + static void print_ucode_info(int old_rev, int new_rev, unsigned int date) 313 309 { 314 - pr_info_once("microcode updated early to revision 0x%x, date = %04x-%02x-%02x\n", 315 - uci->cpu_sig.rev, 310 + pr_info_once("updated early: 0x%x -> 0x%x, date = %04x-%02x-%02x\n", 311 + old_rev, 312 + new_rev, 316 313 date & 0xffff, 317 314 date >> 24, 318 315 (date >> 16) & 0xff); ··· 319 322 320 323 static int delay_ucode_info; 321 324 static int current_mc_date; 325 + static int early_old_rev; 322 326 323 327 /* 324 328 * Print early updated ucode info after printk works. This is delayed info dump. ··· 330 332 331 333 if (delay_ucode_info) { 332 334 intel_cpu_collect_info(&uci); 333 - print_ucode_info(&uci, current_mc_date); 335 + print_ucode_info(early_old_rev, uci.cpu_sig.rev, current_mc_date); 334 336 delay_ucode_info = 0; 335 337 } 336 338 } ··· 339 341 * At this point, we can not call printk() yet. Delay printing microcode info in 340 342 * show_ucode_info_early() until printk() works. 341 343 */ 342 - static void print_ucode(struct ucode_cpu_info *uci) 344 + static void print_ucode(int old_rev, int new_rev, int date) 343 345 { 344 - struct microcode_intel *mc; 345 346 int *delay_ucode_info_p; 346 347 int *current_mc_date_p; 347 - 348 - mc = uci->mc; 349 - if (!mc) 350 - return; 348 + int *early_old_rev_p; 351 349 352 350 delay_ucode_info_p = (int *)__pa_nodebug(&delay_ucode_info); 353 351 current_mc_date_p = (int *)__pa_nodebug(&current_mc_date); 352 + early_old_rev_p = (int *)__pa_nodebug(&early_old_rev); 354 353 355 354 *delay_ucode_info_p = 1; 356 - *current_mc_date_p = mc->hdr.date; 355 + *current_mc_date_p = date; 356 + *early_old_rev_p = old_rev; 357 357 } 358 358 #else 359 359 360 - static inline void print_ucode(struct ucode_cpu_info *uci) 360 + static inline void print_ucode(int old_rev, int new_rev, int date) 361 361 { 362 - struct microcode_intel *mc; 363 - 364 - mc = uci->mc; 365 - if (!mc) 366 - return; 367 - 368 - print_ucode_info(uci, mc->hdr.date); 362 + print_ucode_info(old_rev, new_rev, date); 369 363 } 370 364 #endif 371 365 372 366 static int apply_microcode_early(struct ucode_cpu_info *uci, bool early) 373 367 { 374 368 struct microcode_intel *mc; 375 - u32 rev; 369 + u32 rev, old_rev; 376 370 377 371 mc = uci->mc; 378 372 if (!mc) ··· 380 390 uci->cpu_sig.rev = rev; 381 391 return UCODE_OK; 382 392 } 393 + 394 + old_rev = rev; 383 395 384 396 /* 385 397 * Writeback and invalidate caches before updating microcode to avoid ··· 399 407 uci->cpu_sig.rev = rev; 400 408 401 409 if (early) 402 - print_ucode(uci); 410 + print_ucode(old_rev, uci->cpu_sig.rev, mc->hdr.date); 403 411 else 404 - print_ucode_info(uci, mc->hdr.date); 412 + print_ucode_info(old_rev, uci->cpu_sig.rev, mc->hdr.date); 405 413 406 414 return 0; 407 415 }