Skip to contentSkip to conversation

rework of the microcode splitup

PATCH In review Dmitry Adamushko 3 participants 6 messages Sep 14, 2008 – Sep 17, 2008 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rework of the microcode splitup

Ingo, Peter,

this version is slightly different from the current one in -tip (added missing vfree()).

I have tested it only with "microcode_ctl" (update via /dev/cpu/microcode).

Since my CPUs already have the revision equal to the recent microcode revision from Intel, I temporarily altered the logic of checks in order to verify that the correct microcode file is found and applied. That proved to work well.

Peter: please take a look at the "microcode_amd" part, esp. 2 "FIXME" notes.

todo (not dependent on this patch):
- perhaps, move arch-specific structures from microcode.h to arch-specific *.c files;
- remove union in struct ucode_cpu_info;
- see how we could apply microcode as early as possible;
- don't use set_cpus_allowed()

anyway, I think this new interface is cleaner and better maintainability-wise.

4 files changed, 304 insertions(+), 362 deletions(-)

---

From: Dmitry Adamushko <[email protected]>
Subject: x86-microcode, rework of the microcode splitup

x86, microcode rework, v2

this is a rework of the microcode splitup in tip/x86/microcode

This new interface is supposed to be cleaner and better maintainability-wise;

4 files changed, 304 insertions(+), 362 deletions(-)
arch/x86/kernel/microcode.c +38 -53
@@ -70,8 +70,9 @@
  *		Fix sigmatch() macro to handle old CPUs with pf == 0.
  *		Thanks to Stuart Swales for pointing out this bug.
  */
-
-/* #define DEBUG pr_debug */
+#if 0
+#define DEBUG /* pr_debug */
+#endif
 #include <linux/capability.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
@@ -110,50 +111,28 @@ struct ucode_cpu_info ucode_cpu_info[NR_CPUS];
 EXPORT_SYMBOL_GPL(ucode_cpu_info);
 
 #ifdef CONFIG_MICROCODE_OLD_INTERFACE
-void __user *user_buffer;		/* user area microcode data buffer */
-EXPORT_SYMBOL_GPL(user_buffer);
-unsigned int user_buffer_size;		/* it's size */
-EXPORT_SYMBOL_GPL(user_buffer_size);
-
-static int do_microcode_update(void)
+static int do_microcode_update(const void __user *buf, size_t size)
 {
-	long cursor = 0;
+	cpumask_t old;
 	int error = 0;
-	void *new_mc = NULL;
 	int cpu;
-	cpumask_t old;
 
 	old = current->cpus_allowed;
 
-	while ((cursor = microcode_ops->get_next_ucode(&new_mc, cursor)) > 0) {
-		if (microcode_ops->microcode_sanity_check != NULL)
-			error = microcode_ops->microcode_sanity_check(new_mc);
-		if (error)
+	for_each_online_cpu(cpu) {
+		struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+
+		if (!uci->valid)
+			continue;
+
+		set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
+		error = microcode_ops->request_microcode_user(cpu, buf, size);
+		if (error < 0)
 			goto out;
-		/*
-		 * It's possible the data file has multiple matching ucode,
-		 * lets keep searching till the latest version
-		 */
-		for_each_online_cpu(cpu) {
-			struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
-
-			if (!uci->valid)
-				continue;
-			set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
-			error = microcode_ops->get_matching_microcode(new_mc,
-								      cpu);
-			if (error < 0)
-				goto out;
-			if (error == 1)
-				microcode_ops->apply_microcode(cpu);
-		}
-		vfree(new_mc);
+		if (!error)
+			microcode_ops->apply_microcode(cpu);
 	}
 out:
-	if (cursor > 0)
-		vfree(new_mc);
-	if (cursor < 0)
-		error = cursor;
 	set_cpus_allowed_ptr(current, &old);
 	return error;
 }
@@ -178,10 +157,7 @@ static ssize_t microcode_write(struct file *file, const char __user *buf,
 	get_online_cpus();
 	mutex_lock(&microcode_mutex);
 
-	user_buffer = (void __user *) buf;
-	user_buffer_size = (int) len;
-
-	ret = do_microcode_update();
+	ret = do_microcode_update(buf, len);
 	if (!ret)
 		ret = (ssize_t)len;
 
@@ -231,7 +207,6 @@ MODULE_ALIAS_MISCDEV(MICROCODE_MINOR);
 
 /* fake device for request_firmware */
 struct platform_device *microcode_pdev;
-EXPORT_SYMBOL_GPL(microcode_pdev);
 
 static ssize_t reload_store(struct sys_device *dev,
 			    struct sysdev_attribute *attr,
@@ -252,8 +227,12 @@ static ssize_t reload_store(struct sys_device *dev,
 		if (cpu_online(cpu)) {
 			set_cpus_allowed_ptr(current, &cpumask_of_cpu(cpu));
 			mutex_lock(&microcode_mutex);
-			if (uci->valid)
-				err = microcode_ops->cpu_request_microcode(cpu);
+			if (uci->valid) {
+				err = microcode_ops->request_microcode_fw(cpu,
+						&microcode_pdev->dev);
+				if (!err)
+					microcode_ops->apply_microcode(cpu);
+			}
 			mutex_unlock(&microcode_mutex);
 			set_cpus_allowed_ptr(current, &old);
 		}
@@ -315,7 +294,7 @@ static void collect_cpu_info(int cpu)
 		uci->valid = 1;
 }
 
-static void microcode_resume_cpu(int cpu)
+static int microcode_resume_cpu(int cpu)
 {
 	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
 	struct cpu_signature nsig;
@@ -323,7 +302,7 @@ static void microcode_resume_cpu(int cpu)
 	pr_debug("microcode: CPU%d resumed\n", cpu);
 
 	if (!uci->mc.valid_mc)
-		return;
+		return 1;
 
 	/*
 	 * Let's verify that the 'cached' ucode does belong
@@ -331,21 +310,22 @@ static void microcode_resume_cpu(int cpu)
 	 */
 	if (microcode_ops->collect_cpu_info(cpu, &nsig)) {
 		microcode_fini_cpu(cpu);
-		return;
+		return -1;
 	}
 
 	if (memcmp(&nsig, &uci->cpu_sig, sizeof(nsig))) {
 		microcode_fini_cpu(cpu);
 		/* Should we look for a new ucode here? */
-		return;
+		return 1;
 	}
 
-	microcode_ops->apply_microcode(cpu);
+	return 0;
 }
 
 void microcode_update_cpu(int cpu)
 {
 	struct ucode_cpu_info *uci = ucode_cpu_info + cpu;
+	int err = 0;
 
 	/* We should bind the task to the CPU */
 	BUG_ON(raw_smp_processor_id() != cpu);
@@ -356,12 +336,17 @@ void microcode_update_cpu(int cpu)
 	 * otherwise just request a firmware:
 	 */
 	if (uci->valid) {
-		microcode_resume_cpu(cpu);
+		err = microcode_resume_cpu(cpu);
 	} else {	
 		collect_cpu_info(cpu);
 		if (uci->valid && system_state == SYSTEM_RUNNING)
-			microcode_ops->cpu_request_microcode(cpu);
+			err = microcode_ops->request_microcode_fw(cpu,
+					&microcode_pdev->dev);
 	}
+
+	if (!err)
+		microcode_ops->apply_microcode(cpu);
+
 	mutex_unlock(&microcode_mutex);
 }
 
@@ -412,9 +397,9 @@ static int mc_sysdev_resume(struct sys_device *dev)
 
 	if (!cpu_online(cpu))
 		return 0;
-	pr_debug("microcode: CPU%d resumed\n", cpu);
+
 	/* only CPU 0 will apply ucode here */
-	microcode_ops->apply_microcode(0);
+	microcode_update_cpu(0);
 	return 0;
 }
 
arch/x86/kernel/microcode_amd.c +157 -174

420 lines not on this page · raw message

arch/x86/kernel/microcode_intel.c +101 -126

308 lines not on this page · raw message

include/asm-x86/microcode.h +8 -9
index 7ceff48..e2887fa 100644
@@ -5,17 +5,16 @@ extern int microcode_init(void *opaque, struct module *module);
 extern void microcode_exit(void);
 
 struct cpu_signature;
+struct device;
 
 struct microcode_ops {
-	long (*get_next_ucode)(void **mc, long offset);
-	long (*microcode_get_next_ucode)(void **mc, long offset);
-	int (*get_matching_microcode)(void *mc, int cpu);
-	int (*microcode_sanity_check)(void *mc);
-	int (*cpu_request_microcode)(int cpu);
-	int (*collect_cpu_info)(int cpu_num, struct cpu_signature *csig);
-	void (*apply_microcode)(int cpu);
-	void (*microcode_fini_cpu)(int cpu);
-	void (*clear_patch)(void *data);
+	int  (*request_microcode_user) (int cpu, const void __user *buf, size_t size);
+	int  (*request_microcode_fw) (int cpu, struct device *device);
+
+	void (*apply_microcode) (int cpu);
+
+	int  (*collect_cpu_info) (int cpu, struct cpu_signature *csig);
+	void (*microcode_fini_cpu) (int cpu);
 };
 
 struct microcode_header_intel {
#1 lore raw

[ forgot to add my SOB line ]

Quoted 11 lines

From: Dmitry Adamushko <[email protected]>
Subject: x86-microcode, rework of the microcode splitup

x86, microcode rework, v2

this is a rework of the microcode splitup in tip/x86/microcode

This new interface is supposed to be cleaner and better maintainability-wise;

4 files changed, 304 insertions(+), 362 deletions(-)
Quoted 9 lines
arch/x86/kernel/microcode.c +0 -1
@@ -70,8 +70,9 @@
 *             Fix sigmatch() macro to handle old CPUs with pf == 0.
 *             Thanks to Stuart Swales for pointing out this bug.
 */
-

[ ... ]

Signature · 2 lines
Best regards,
Dmitry Adamushko
#2 lore raw
In reply to Dmitry Adamushko
Quoted 5 lines · Dmitry Adamushko

Ingo, Peter,

this version is slightly different from the current one in -tip (added missing vfree()).

i've added the delta patch below. I removed the #if 0's as they are ugly. (and editing the file to enable debug printouts is easy anyway)

regarding this FIXME:

+ /* FIXME! dimm: Why not by means of get_totalsize(hdr)? */

        total_size = (unsigned long) (hdr[4] + (hdr[5] << 8));

please just use get_totalsize(hdr) and get rid of the fixme that way :)

Ingo

------------------>

Quoted 1 line

From a1c75cc5018f17ff6d80ce45a13435b1536f76db Mon Sep 17 00:00:00 2001

From: Ingo Molnar <[email protected]>
Date: Sun, 14 Sep 2008 14:50:26 +0200
Subject: [PATCH] x86, microcode rework, v2, fix

based on patch from Dmitry Adamushko.

- add missing vfree()
- update debug printks

---
 arch/x86/kernel/microcode.c       |    4 +---
 arch/x86/kernel/microcode_amd.c   |    6 ++++--
 arch/x86/kernel/microcode_intel.c |    4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/microcode.c +1 -3
index 902dada..0c2634f 100644
@@ -70,8 +70,6 @@
  *		Fix sigmatch() macro to handle old CPUs with pf == 0.
  *		Thanks to Stuart Swales for pointing out this bug.
  */
-
-/* #define DEBUG pr_debug */
 #include <linux/capability.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
@@ -396,7 +394,7 @@ static int mc_sysdev_resume(struct sys_device *dev)
 
 	if (!cpu_online(cpu))
 		return 0;
-	pr_debug("microcode: CPU%d resumed\n", cpu);
+
 	/* only CPU 0 will apply ucode here */
 	microcode_update_cpu(0);
 	return 0;
arch/x86/kernel/microcode_amd.c +4 -2
index 6815837..48aec9f 100644
@@ -92,7 +92,7 @@ static int get_matching_microcode(int cpu, void *mc, int rev)
 	unsigned int i = 0;
 
 	/*
-	 * dimm: do we need this? Why an update via /dev/... is different
+	 * FIXME! dimm: do we need this? Why an update via /dev/... is different
 	 * from the one via firmware?
 	 *
 	 * This is a tricky part. We might be called from a write operation
@@ -246,7 +246,7 @@ static void * get_next_ucode(u8 *buf, unsigned int size,
 		return NULL;
 	}
 
-	/* Why not by means of get_totalsize(hdr)? */
+	/* FIXME! dimm: Why not by means of get_totalsize(hdr)? */
 	total_size = (unsigned long) (hdr[4] + (hdr[5] << 8));
 
 	printk(KERN_INFO "microcode: size %u, total_size %u\n",
@@ -342,6 +342,8 @@ static int generic_load_microcode(int cpu, void *data, size_t size,
 
 		mc_header = (struct microcode_header_amd *)mc;
 		if (get_matching_microcode(cpu, mc, new_rev)) {
+			if (new_mc)
+				vfree(new_mc);
 			new_rev = mc_header->patch_id;
 			new_mc  = mc;
 		} else 
arch/x86/kernel/microcode_intel.c +2 -2
index f4930b5..48ed3ce 100644
@@ -70,8 +70,6 @@
  *		Fix sigmatch() macro to handle old CPUs with pf == 0.
  *		Thanks to Stuart Swales for pointing out this bug.
  */
-
-/* #define DEBUG */ /* pr_debug */
 #include <linux/capability.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
@@ -356,6 +354,8 @@ static int generic_load_microcode(int cpu, void *data, size_t size,
 		}
 
 		if (get_matching_microcode(&uci->cpu_sig, mc, new_rev)) {
+			if (new_mc)
+				vfree(new_mc);
 			new_rev = mc_header.rev;
 			new_mc  = mc;
 		} else
#3 lore raw

2008/9/14 Ingo Molnar <[email protected]>:

Quoted 11 lines · Dmitry Adamushko
Quoted 6 lines

Ingo, Peter,

this version is slightly different from the current one in -tip (added missing vfree()).

i've added the delta patch below. I removed the #if 0's as they are ugly. (and editing the file to enable debug printouts is easy anyway)

ok, thanks.

Quoted 7 lines

regarding this FIXME:

+ /* FIXME! dimm: Why not by means of get_totalsize(hdr)? */

       total_size = (unsigned long) (hdr[4] + (hdr[5] << 8));

please just use get_totalsize(hdr) and get rid of the fixme that way :)

The thing is that it looks like there are 2 headers that contain the size. 'hdr' taken by get_totalsize() is not the same as 'hdr' from which total_size is calculated here. :^)

A quick search didn't reveal any docs describing the AMD microcode file format, so I'd rather leave it up to Peter.

Quoted 3 lines

Ingo

Signature · 2 lines
Best regards,
Dmitry Adamushko
#4 lore raw

PATCH 1/1 x86, microcode rework, v2, renaming cont.

Renaming based on patch from Dmitry Adamushko.

Further clarification by renaming define and variable related to microcode container file.

---
 arch/x86/kernel/microcode_amd.c |   17 ++++++++---------
 1 files changed, 8 insertions(+), 9 deletions(-)
arch/x86/kernel/microcode_amd.c +8 -9
index 8d97840..113be63 100644
@@ -233,21 +233,20 @@ static void * get_next_ucode(u8 *buf, unsigned int size,
 			unsigned int *mc_size)
 {
 	unsigned int total_size;
-#define UCODE_UNKNOWN_HDR	8
-	u8 hdr[UCODE_UNKNOWN_HDR];
+#define UCODE_CONTAINER_SECTION_HDR	8
+	u8 section_hdr[UCODE_CONTAINER_SECTION_HDR];
 	void *mc;
 
-	if (get_ucode_data(hdr, buf, UCODE_UNKNOWN_HDR))
+	if (get_ucode_data(section_hdr, buf, UCODE_CONTAINER_SECTION_HDR))
 		return NULL;
 
-	if (hdr[0] != UCODE_UCODE_TYPE) {
+	if (section_hdr[0] != UCODE_UCODE_TYPE) {
 		printk(KERN_ERR "microcode: error! "
 		       "Wrong microcode payload type field\n");
 		return NULL;
 	}
 
-	/* FIXME! dimm: Why not by means of get_totalsize(hdr)? */
-	total_size = (unsigned long) (hdr[4] + (hdr[5] << 8));
+	total_size = (unsigned long) (section_hdr[4] + (section_hdr[5] << 8));
 
 	printk(KERN_INFO "microcode: size %u, total_size %u\n",
 		size, total_size);
@@ -260,13 +259,13 @@ static void * get_next_ucode(u8 *buf, unsigned int size,
 	mc = vmalloc(UCODE_MAX_SIZE);
 	if (mc) {
 		memset(mc, 0, UCODE_MAX_SIZE);
-		if (get_ucode_data(mc, buf + UCODE_UNKNOWN_HDR, total_size)) {
+		if (get_ucode_data(mc, buf + UCODE_CONTAINER_SECTION_HDR, total_size)) {
 			vfree(mc);
 			mc = NULL;
 		} else
-			*mc_size = total_size + UCODE_UNKNOWN_HDR;
+			*mc_size = total_size + UCODE_CONTAINER_SECTION_HDR;
 	}
-#undef UCODE_UNKNOWN_HDR
+#undef UCODE_CONTAINER_SECTION_HDR
 	return mc;
 }
 
Signature · 1 line
1.5.4.1
#5 lore raw

PATCH 1/1 x86, microcode rework, v2, renaming

In reply to Dmitry Adamushko

Renaming based on patch from Dmitry Adamushko.

Made code more readable by renaming define and variables related to microcode _container_file_ header to make it distinguishable from microcode _patch_ header.

---
 arch/x86/kernel/microcode_amd.c |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/kernel/microcode_amd.c +7 -7
index 48aec9f..8d97840 100644
@@ -274,12 +274,12 @@ static void * get_next_ucode(u8 *buf, unsigned int size,
 static int install_equiv_cpu_table(u8 *buf,
 		int (*get_ucode_data)(void *, const void *, size_t))
 {
-#define UCODE_HEADER_SIZE	12
-	u8 *hdr[UCODE_HEADER_SIZE];
-	unsigned int *buf_pos = (unsigned int *)hdr;
+#define UCODE_CONTAINER_HEADER_SIZE	12
+	u8 *container_hdr[UCODE_CONTAINER_HEADER_SIZE];
+	unsigned int *buf_pos = (unsigned int *)container_hdr;
 	unsigned long size;
 
-	if (get_ucode_data(&hdr, buf, UCODE_HEADER_SIZE))
+	if (get_ucode_data(&container_hdr, buf, UCODE_CONTAINER_HEADER_SIZE))
 		return 0;
 
 	size = buf_pos[2];
@@ -296,14 +296,14 @@ static int install_equiv_cpu_table(u8 *buf,
 		return 0;
 	}
 
-	buf += UCODE_HEADER_SIZE;
+	buf += UCODE_CONTAINER_HEADER_SIZE;
 	if (get_ucode_data(equiv_cpu_table, buf, size)) {
 		vfree(equiv_cpu_table);
 		return 0;
 	}
 
-	return size + UCODE_HEADER_SIZE; /* add header length */
-#undef UCODE_HEADER_SIZE
+	return size + UCODE_CONTAINER_HEADER_SIZE; /* add header length */
+#undef UCODE_CONTAINER_HEADER_SIZE
 }
 
 static void free_equiv_cpu_table(void)
Signature · 1 line
1.5.4.1