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:
"Update documentation, improve robustness and fix a memory leak"

* 'x86-microcode-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
x86/microcode/intel: Improve microcode patches saving flow
x86/microcode: Document the three loading methods
x86/microcode/AMD: Free unneeded patch before exit from update_cache()

+155 -84
-70
Documentation/x86/early-microcode.txt
··· 1 - Early load microcode 2 - ==================== 3 - By Fenghua Yu <fenghua.yu@intel.com> 4 - 5 - Kernel can update microcode in early phase of boot time. Loading microcode early 6 - can fix CPU issues before they are observed during kernel boot time. 7 - 8 - Microcode is stored in an initrd file. The microcode is read from the initrd 9 - file and loaded to CPUs during boot time. 10 - 11 - The format of the combined initrd image is microcode in cpio format followed by 12 - the initrd image (maybe compressed). Kernel parses the combined initrd image 13 - during boot time. The microcode file in cpio name space is: 14 - on Intel: kernel/x86/microcode/GenuineIntel.bin 15 - on AMD : kernel/x86/microcode/AuthenticAMD.bin 16 - 17 - During BSP boot (before SMP starts), if the kernel finds the microcode file in 18 - the initrd file, it parses the microcode and saves matching microcode in memory. 19 - If matching microcode is found, it will be uploaded in BSP and later on in all 20 - APs. 21 - 22 - The cached microcode patch is applied when CPUs resume from a sleep state. 23 - 24 - There are two legacy user space interfaces to load microcode, either through 25 - /dev/cpu/microcode or through /sys/devices/system/cpu/microcode/reload file 26 - in sysfs. 27 - 28 - In addition to these two legacy methods, the early loading method described 29 - here is the third method with which microcode can be uploaded to a system's 30 - CPUs. 31 - 32 - The following example script shows how to generate a new combined initrd file in 33 - /boot/initrd-3.5.0.ucode.img with original microcode microcode.bin and 34 - original initrd image /boot/initrd-3.5.0.img. 35 - 36 - mkdir initrd 37 - cd initrd 38 - mkdir -p kernel/x86/microcode 39 - cp ../microcode.bin kernel/x86/microcode/GenuineIntel.bin (or AuthenticAMD.bin) 40 - find . | cpio -o -H newc >../ucode.cpio 41 - cd .. 42 - cat ucode.cpio /boot/initrd-3.5.0.img >/boot/initrd-3.5.0.ucode.img 43 - 44 - Builtin microcode 45 - ================= 46 - 47 - We can also load builtin microcode supplied through the regular firmware 48 - builtin method CONFIG_FIRMWARE_IN_KERNEL. Only 64-bit is currently 49 - supported. 50 - 51 - Here's an example: 52 - 53 - CONFIG_FIRMWARE_IN_KERNEL=y 54 - CONFIG_EXTRA_FIRMWARE="intel-ucode/06-3a-09 amd-ucode/microcode_amd_fam15h.bin" 55 - CONFIG_EXTRA_FIRMWARE_DIR="/lib/firmware" 56 - 57 - This basically means, you have the following tree structure locally: 58 - 59 - /lib/firmware/ 60 - |-- amd-ucode 61 - ... 62 - | |-- microcode_amd_fam15h.bin 63 - ... 64 - |-- intel-ucode 65 - ... 66 - | |-- 06-3a-09 67 - ... 68 - 69 - so that the build system can find those files and integrate them into 70 - the final kernel image. The early loader finds them and applies them.
···
+137
Documentation/x86/microcode.txt
···
··· 1 + The Linux Microcode Loader 2 + 3 + Authors: Fenghua Yu <fenghua.yu@intel.com> 4 + Borislav Petkov <bp@suse.de> 5 + 6 + The kernel has a x86 microcode loading facility which is supposed to 7 + provide microcode loading methods in the OS. Potential use cases are 8 + updating the microcode on platforms beyond the OEM End-Of-Life support, 9 + and updating the microcode on long-running systems without rebooting. 10 + 11 + The loader supports three loading methods: 12 + 13 + 1. Early load microcode 14 + ======================= 15 + 16 + The kernel can update microcode very early during boot. Loading 17 + microcode early can fix CPU issues before they are observed during 18 + kernel boot time. 19 + 20 + The microcode is stored in an initrd file. During boot, it is read from 21 + it and loaded into the CPU cores. 22 + 23 + The format of the combined initrd image is microcode in (uncompressed) 24 + cpio format followed by the (possibly compressed) initrd image. The 25 + loader parses the combined initrd image during boot. 26 + 27 + The microcode files in cpio name space are: 28 + 29 + on Intel: kernel/x86/microcode/GenuineIntel.bin 30 + on AMD : kernel/x86/microcode/AuthenticAMD.bin 31 + 32 + During BSP (BootStrapping Processor) boot (pre-SMP), the kernel 33 + scans the microcode file in the initrd. If microcode matching the 34 + CPU is found, it will be applied in the BSP and later on in all APs 35 + (Application Processors). 36 + 37 + The loader also saves the matching microcode for the CPU in memory. 38 + Thus, the cached microcode patch is applied when CPUs resume from a 39 + sleep state. 40 + 41 + Here's a crude example how to prepare an initrd with microcode (this is 42 + normally done automatically by the distribution, when recreating the 43 + initrd, so you don't really have to do it yourself. It is documented 44 + here for future reference only). 45 + 46 + --- 47 + #!/bin/bash 48 + 49 + if [ -z "$1" ]; then 50 + echo "You need to supply an initrd file" 51 + exit 1 52 + fi 53 + 54 + INITRD="$1" 55 + 56 + DSTDIR=kernel/x86/microcode 57 + TMPDIR=/tmp/initrd 58 + 59 + rm -rf $TMPDIR 60 + 61 + mkdir $TMPDIR 62 + cd $TMPDIR 63 + mkdir -p $DSTDIR 64 + 65 + if [ -d /lib/firmware/amd-ucode ]; then 66 + cat /lib/firmware/amd-ucode/microcode_amd*.bin > $DSTDIR/AuthenticAMD.bin 67 + fi 68 + 69 + if [ -d /lib/firmware/intel-ucode ]; then 70 + cat /lib/firmware/intel-ucode/* > $DSTDIR/GenuineIntel.bin 71 + fi 72 + 73 + find . | cpio -o -H newc >../ucode.cpio 74 + cd .. 75 + mv $INITRD $INITRD.orig 76 + cat ucode.cpio $INITRD.orig > $INITRD 77 + 78 + rm -rf $TMPDIR 79 + --- 80 + 81 + The system needs to have the microcode packages installed into 82 + /lib/firmware or you need to fixup the paths above if yours are 83 + somewhere else and/or you've downloaded them directly from the processor 84 + vendor's site. 85 + 86 + 2. Late loading 87 + =============== 88 + 89 + There are two legacy user space interfaces to load microcode, either through 90 + /dev/cpu/microcode or through /sys/devices/system/cpu/microcode/reload file 91 + in sysfs. 92 + 93 + The /dev/cpu/microcode method is deprecated because it needs a special 94 + userspace tool for that. 95 + 96 + The easier method is simply installing the microcode packages your distro 97 + supplies and running: 98 + 99 + # echo 1 > /sys/devices/system/cpu/microcode/reload 100 + 101 + as root. 102 + 103 + The loading mechanism looks for microcode blobs in 104 + /lib/firmware/{intel-ucode,amd-ucode}. The default distro installation 105 + packages already put them there. 106 + 107 + 3. Builtin microcode 108 + ==================== 109 + 110 + The loader supports also loading of a builtin microcode supplied through 111 + the regular firmware builtin method CONFIG_FIRMWARE_IN_KERNEL. Only 112 + 64-bit is currently supported. 113 + 114 + Here's an example: 115 + 116 + CONFIG_FIRMWARE_IN_KERNEL=y 117 + CONFIG_EXTRA_FIRMWARE="intel-ucode/06-3a-09 amd-ucode/microcode_amd_fam15h.bin" 118 + CONFIG_EXTRA_FIRMWARE_DIR="/lib/firmware" 119 + 120 + This basically means, you have the following tree structure locally: 121 + 122 + /lib/firmware/ 123 + |-- amd-ucode 124 + ... 125 + | |-- microcode_amd_fam15h.bin 126 + ... 127 + |-- intel-ucode 128 + ... 129 + | |-- 06-3a-09 130 + ... 131 + 132 + so that the build system can find those files and integrate them into 133 + the final kernel image. The early loader finds them and applies them. 134 + 135 + Needless to say, this method is not the most flexible one because it 136 + requires rebuilding the kernel each time updated microcode from the CPU 137 + vendor is available.
+4 -1
arch/x86/kernel/cpu/microcode/amd.c
··· 400 401 list_for_each_entry(p, &microcode_cache, plist) { 402 if (p->equiv_cpu == new_patch->equiv_cpu) { 403 - if (p->patch_id >= new_patch->patch_id) 404 /* we already have the latest patch */ 405 return; 406 407 list_replace(&p->plist, &new_patch->plist); 408 kfree(p->data);
··· 400 401 list_for_each_entry(p, &microcode_cache, plist) { 402 if (p->equiv_cpu == new_patch->equiv_cpu) { 403 + if (p->patch_id >= new_patch->patch_id) { 404 /* we already have the latest patch */ 405 + kfree(new_patch->data); 406 + kfree(new_patch); 407 return; 408 + } 409 410 list_replace(&p->plist, &new_patch->plist); 411 kfree(p->data);
+14 -13
arch/x86/kernel/cpu/microcode/intel.c
··· 146 return false; 147 } 148 149 - static struct ucode_patch *__alloc_microcode_buf(void *data, unsigned int size) 150 { 151 struct ucode_patch *p; 152 153 p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL); 154 if (!p) 155 - return ERR_PTR(-ENOMEM); 156 157 p->data = kmemdup(data, size, GFP_KERNEL); 158 if (!p->data) { 159 kfree(p); 160 - return ERR_PTR(-ENOMEM); 161 } 162 163 return p; ··· 183 if (mc_hdr->rev <= mc_saved_hdr->rev) 184 continue; 185 186 - p = __alloc_microcode_buf(data, size); 187 - if (IS_ERR(p)) 188 pr_err("Error allocating buffer %p\n", data); 189 else 190 list_replace(&iter->plist, &p->plist); ··· 196 * newly found. 197 */ 198 if (!prev_found) { 199 - p = __alloc_microcode_buf(data, size); 200 - if (IS_ERR(p)) 201 pr_err("Error allocating buffer for %p\n", data); 202 else 203 list_add_tail(&p->plist, &microcode_cache); 204 } 205 206 /* 207 * Save for early loading. On 32-bit, that needs to be a physical 208 * address as the APs are running from physical addresses, before 209 * paging has been enabled. 210 */ 211 - if (p) { 212 - if (IS_ENABLED(CONFIG_X86_32)) 213 - intel_ucode_patch = (struct microcode_intel *)__pa_nodebug(p->data); 214 - else 215 - intel_ucode_patch = p->data; 216 - } 217 } 218 219 static int microcode_sanity_check(void *mc, int print_err)
··· 146 return false; 147 } 148 149 + static struct ucode_patch *memdup_patch(void *data, unsigned int size) 150 { 151 struct ucode_patch *p; 152 153 p = kzalloc(sizeof(struct ucode_patch), GFP_KERNEL); 154 if (!p) 155 + return NULL; 156 157 p->data = kmemdup(data, size, GFP_KERNEL); 158 if (!p->data) { 159 kfree(p); 160 + return NULL; 161 } 162 163 return p; ··· 183 if (mc_hdr->rev <= mc_saved_hdr->rev) 184 continue; 185 186 + p = memdup_patch(data, size); 187 + if (!p) 188 pr_err("Error allocating buffer %p\n", data); 189 else 190 list_replace(&iter->plist, &p->plist); ··· 196 * newly found. 197 */ 198 if (!prev_found) { 199 + p = memdup_patch(data, size); 200 + if (!p) 201 pr_err("Error allocating buffer for %p\n", data); 202 else 203 list_add_tail(&p->plist, &microcode_cache); 204 } 205 + 206 + if (!p) 207 + return; 208 209 /* 210 * Save for early loading. On 32-bit, that needs to be a physical 211 * address as the APs are running from physical addresses, before 212 * paging has been enabled. 213 */ 214 + if (IS_ENABLED(CONFIG_X86_32)) 215 + intel_ucode_patch = (struct microcode_intel *)__pa_nodebug(p->data); 216 + else 217 + intel_ucode_patch = p->data; 218 } 219 220 static int microcode_sanity_check(void *mc, int print_err)