Skip to contentSkip to conversation

microcode: Scan the multiboot payloads for cpio format microcode blob. (v3.3)

PATCH In review Konrad Rzeszutek Wilk 2 participants 4 messages Sep 27, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 microcode: Scan the multiboot payloads for cpio format microcode blob. (v3.3)

<blurb from v1>:
Please see the following patch which implements a mechanism to scan the initramfs for the format of an microcode files. This is a feature that the Linux kernel has since v3.10 - where it searches in the initramfs for an archive of the microcode blob. The format is documented in the Linux tree and the commit description contains it.

The tool to make this work is the initramfs creator. The one tool 'dracut' has support for this via the '--early-microcode' parameter. (See http://news.gmane.org/gmane.linux.kernel.initramfs for dracut 030 announcement))

That, along with this patch, allows the Xen hypervisor to update the microcode during bootup. Please review attached patch.

I had also tested just using Linux how well it deals with an initramfs composed of two cpio images. Testing revealed that it worked great even if the kernel did not have the early cpio support build in. David Vrabel pointed out that it b/c:
"The kernel unpacks all cpio archives it finds in the initramfs image so the kernel doesn't have to be aware of the way tools have packed the filesystem into different cpio archive."

The way to use this is by the 'ucode' parameter. It has now two meanings:
[<index>|initrd]

Which CANNOT be used together. By default this auto scanning is turned off as Jan pointed out that: "Xen otoh has to be careful not to mis-interpret a blob passed to a non-Linux Dom0 as a CPIO. How good the guarding against this is in the code I'll have to check".

The author would like to have this on by default but that can wait till a later time when maintainer is comfortable with this being on by default.
</blurb from v1>

There is also the question whether the parameter should be 'cpio','initrd' or 'scan'. As in the future the extraction of the payload could be from a different format than the cpio (say a microcode blob with an magic string at the start). The author believes that at that time the logic to scan the mulitboot payloads can be expanded to also scan formats other than cpio format. Jan (who initially raised this question) in the last review did not comment on the choice so I think it is left to Keir (if he cares about the distinction).

Please apply!

 docs/misc/xen-command-line.markdown |   14 ++-
 xen/arch/x86/microcode.c            |  175 +++++++++++++++++++++++++++++++---
 xen/common/Makefile                 |    2 +-
 xen/common/earlycpio.c              |  151 ++++++++++++++++++++++++++++++
 xen/include/xen/earlycpio.h         |   14 +++
 5 files changed, 336 insertions(+), 20 deletions(-)

Konrad Rzeszutek Wilk (2):
microcode: Scan the initramfs payload for microcode blob.
microcode: Check whether the microcode is correct.

#1 lore raw

PATCH 1/2 microcode: Scan the initramfs payload for microcode blob.

The Linux kernel is able to update the microcode during early bootup via inspection of the initramfs blob to see if there is an cpio image with certain microcode files. Linux is able to function with two (or more) cpio archives in the initrd b/c it unpacks all of the cpio archives.

The format of the early initramfs is nicely documented in Linux's Documentation/x86/early-microcode.txt:

Early load microcode
====================
By Fenghua Yu <[email protected]>

Kernel can update microcode in early phase of boot time. Loading microcode early can fix CPU issues before they are observed during kernel boot time.

Microcode is stored in an initrd file. The microcode is read from the initrd file and loaded to CPUs during boot time.

The format of the combined initrd image is microcode in cpio format followed by the initrd image (maybe compressed). Kernel parses the combined initrd image during boot time. The microcode file in cpio name space is:
kernel/x86/microcode/GenuineIntel.bin

During BSP boot (before SMP starts), if the kernel finds the microcode file in the initrd file, it parses the microcode and saves matching microcode in memory. If matching microcode is found, it will be uploaded in BSP and later on in all APs.

The cached microcode patch is applied when CPUs resume from a sleep state.

There are two legacy user space interfaces to load microcode, either through /dev/cpu/microcode or through /sys/devices/system/cpu/microcode/reload file in sysfs.

In addition to these two legacy methods, the early loading method described here is the third method with which microcode can be uploaded to a system's CPUs.

The following example script shows how to generate a new combined initrd file in /boot/initrd-3.5.0.ucode.img with original microcode microcode.bin and original initrd image /boot/initrd-3.5.0.img.

mkdir initrd
cd initrd
mkdir kernel
mkdir kernel/x86
mkdir kernel/x86/microcode
cp ../microcode.bin kernel/x86/microcode/GenuineIntel.bin find .|cpio -oc >../ucode.cpio
cd ..
cat ucode.cpio /boot/initrd-3.5.0.img >/boot/initrd-3.5.0.ucode.img

As such this code inspects the initrd to see if the microcode signatures are present and if so updates the hypervisor.

The option to turn this scan on/off is gated by the 'ucode' parameter. The options are now:
'scan' Scan for the microcode in any multiboot payload.
<index> Attempt to load microcode blob (not the cpio archive
format) from the multiboot payload number.

This option alters slightly the 'ucode' parameter by only allowing either parameter:
ucode=[<index>|scan]

Implementation wise the ucode_blob is defined as __initdata. That is OK from the viewpoint of suspend/resume as the the underlaying architecture microcode (microcode_intel or microcode_amd) end up saving the blob in 'struct ucode_cpu_info' which is a per-cpu data structure (see ucode_cpu_info). They end up saving it when doing the pre-SMP (for CPU0) and SMP (for the rest) microcode loading.

Naturally if one does a hypercall to update the microcode and it is newer, then the old per-cpu data is replaced.

[v1: Revised code based on Boris Ostrovsky's comments]
[v2: Fix memory leak (forgot to call xfree)]

[v3: Added comments from David, stuck the option under ucode per Jan's
review]
[v4: Review comments from Jan]
[v5: Review comments from Andrew]
[v6: Altered docs with Jan's feedback]

---
 docs/misc/xen-command-line.markdown |   14 +++-
 xen/arch/x86/microcode.c            |  158 +++++++++++++++++++++++++++++++----
 xen/common/Makefile                 |    2 +-
 xen/common/earlycpio.c              |  151 +++++++++++++++++++++++++++++++++
 xen/include/xen/earlycpio.h         |   14 +++
 5 files changed, 320 insertions(+), 19 deletions(-)
 create mode 100644 xen/common/earlycpio.c
 create mode 100644 xen/include/xen/earlycpio.h
docs/misc/xen-command-line.markdown +11 -3
index 56c9729..dc72473 100644
@@ -897,10 +897,12 @@ pages) must also be specified via the tbuf\_size parameter.
 > `= unstable | skewed`
 
 ### ucode
-> `= <integer>`
+> `= [<integer> | scan]`
+
+Specify how and where to find CPU microcode update blob.
 
-Specify the CPU microcode update blob module index. When positive, this
-specifies the n-th module (in the GrUB entry, zero based) to be used
+'integer' specifies the CPU microcode update blob module index. When positive,
+this specifies the n-th module (in the GrUB entry, zero based) to be used
 for updating CPU micrcode. When negative, counting starts at the end of
 the modules in the GrUB entry (so with the blob commonly being last,
 one could specify `ucode=-1`). Note that the value of zero is not valid
@@ -910,6 +912,12 @@ when used with xen.efi (there the concept of modules doesn't exist, and
 the blob gets specified via the `ucode=<filename>` config file/section
 entry; see [EFI configuration file description](efi.html)).
 
+'scan' instructs the hypervisor to scan the multiboot images for an cpio
+image that contains microcode. Depending on the platform the blob with the
+microcode in the cpio name space must be:
+  - on Intel: kernel/x86/microcode/GenuineIntel.bin
+  - on AMD  : kernel/x86/microcode/AuthenticAMD.bin
+
 ### unrestricted\_guest
 > `= <boolean>`
 
xen/arch/x86/microcode.c +143 -15

223 lines not on this page · raw message

xen/common/Makefile +1 -1
index 5486140..6da4651 100644
@@ -49,7 +49,7 @@ obj-y += radix-tree.o
 obj-y += rbtree.o
 obj-y += lzo.o
 
-obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo,$(n).init.o)
+obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma unlzo earlycpio,$(n).init.o)
 
 obj-$(perfc)       += perfc.o
 obj-$(crash_debug) += gdbstub.o
xen/common/earlycpio.c new file +151 -0

151 lines not on this page · raw message

xen/include/xen/earlycpio.h new file +14 -0
new file mode 100644
index 0000000..85d144a
@@ -0,0 +1,14 @@
+#ifndef _EARLYCPIO_H
+#define _EARLYCPIO_H
+
+#define MAX_CPIO_FILE_NAME 18
+
+struct cpio_data {
+	void *data;
+	size_t size;
+};
+
+struct cpio_data find_cpio_data(const char *path, void *data, size_t len,
+				long *offset);
+
+#endif /* _EARLYCPIO_H */
Signature · 1 line
1.7.7.6
#2 lore raw

PATCH 2/2 microcode: Check whether the microcode is correct.

In reply to Konrad Rzeszutek Wilk

We do the microcode code update in two steps - the presmp:
'microcode_presmp_init' and when CPUs are brought up:
'microcode_init'. The earlier performs the microcode update on the BSP - but unfortunately it does not check whether the update failed. Which means that we might try later to update a incorrect payload on the rest of CPUs.

This patch handles this odd situation.

[v2: Redid it as we are not doing scan and <index> at the same time]
---
 xen/arch/x86/microcode.c |   17 ++++++++++++++++-
 1 files changed, 16 insertions(+), 1 deletions(-)
xen/arch/x86/microcode.c +16 -1
index e6344cf..091d5d1 100644
@@ -418,6 +418,7 @@ static int __init microcode_presmp_init(void)
         {
             void *data;
             size_t len;
+            int rc = 0;
 
             if ( ucode_blob.size )
             {
@@ -430,10 +431,24 @@ static int __init microcode_presmp_init(void)
                 data = ucode_mod_map(&ucode_mod);
             }
             if ( data )
-                microcode_update_cpu(data, len);
+                rc = microcode_update_cpu(data, len);
+            else
+                rc = -ENOMEM;
 
             if ( !ucode_blob.size )
                 ucode_mod_map(NULL);
+
+            if ( rc )
+            {
+                if ( ucode_blob.size )
+                {
+                    xfree(ucode_blob.data);
+                    ucode_blob.size = 0;
+                    ucode_blob.data = NULL;
+                }
+                else
+                    ucode_mod.mod_end = 0;
+            }
         }
 
         register_cpu_notifier(&microcode_percpu_nfb);
Signature · 1 line
1.7.7.6
Acked-by on v1 1/1
#3 lore raw
In reply to Konrad Rzeszutek Wilk
Quoted 58 lines · Konrad Rzeszutek Wilk

<blurb from v1>:
Please see the following patch which implements a mechanism to scan the initramfs for the format of an microcode files. This is a feature that the Linux kernel has since v3.10 - where it searches in the initramfs for an archive of the microcode blob. The format is documented in the Linux tree and the commit description contains it.

The tool to make this work is the initramfs creator. The one tool 'dracut' has support for this via the '--early-microcode' parameter. (See http://news.gmane.org/gmane.linux.kernel.initramfs for dracut 030 announcement))

That, along with this patch, allows the Xen hypervisor to update the microcode during bootup. Please review attached patch.

I had also tested just using Linux how well it deals with an initramfs composed of two cpio images. Testing revealed that it worked great even if the kernel did not have the early cpio support build in. David Vrabel pointed out that it b/c:
"The kernel unpacks all cpio archives it finds in the initramfs image so the kernel doesn't have to be aware of the way tools have packed the filesystem into different cpio archive."

The way to use this is by the 'ucode' parameter. It has now two meanings:
[<index>|initrd]

Which CANNOT be used together. By default this auto scanning is turned off as Jan pointed out that: "Xen otoh has to be careful not to mis-interpret a blob passed to a non-Linux Dom0 as a CPIO. How good the guarding against this is in the code I'll have to check".

The author would like to have this on by default but that can wait till a later time when maintainer is comfortable with this being on by default.
</blurb from v1>

There is also the question whether the parameter should be 'cpio','initrd' or 'scan'. As in the future the extraction of the payload could be from a different format than the cpio (say a microcode blob with an magic string at the start). The author believes that at that time the logic to scan the mulitboot payloads can be expanded to also scan formats other than cpio format. Jan (who initially raised this question) in the last review did not comment on the choice so I think it is left to Keir (if he cares about the distinction).

Please apply!

 docs/misc/xen-command-line.markdown |   14 ++-
 xen/arch/x86/microcode.c            |  175 +++++++++++++++++++++++++++++++---
 xen/common/Makefile                 |    2 +-
 xen/common/earlycpio.c              |  151 ++++++++++++++++++++++++++++++
 xen/include/xen/earlycpio.h         |   14 +++
 5 files changed, 336 insertions(+), 20 deletions(-)

Konrad Rzeszutek Wilk (2):
microcode: Scan the initramfs payload for microcode blob.
microcode: Check whether the microcode is correct.