public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: Quentin Casasnovas <quentin.casasnovas@oracle.com>
Cc: X86 ML <x86@kernel.org>, LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 07/13] x86/microcode/intel: Rename update_match_revision()
Date: Fri, 10 Apr 2015 13:12:18 +0200	[thread overview]
Message-ID: <20150410111218.GC28074@pd.tnic> (raw)
In-Reply-To: <20150224162318.GG4565@chrystal.uk.oracle.com>

On Tue, Feb 24, 2015 at 05:23:18PM +0100, Quentin Casasnovas wrote:
> Minor nit-pick, if you reverse your inequality, you don't need for the
> ternary operator.

Yeah, so I started looking at that and it seems the rabbit hole goes
deeper.

Let's look at the call to revision_is_newer() in _save_mc():

save_mc:

	new_rev      = mc_hdr->rev;

	...

	if (!revision_is_newer(mc_hdr, new_rev))
	->
	if (!((mc_hdr->rev <= new_rev) ? 0 : 1))
	->
	if (!((mc_hdr->rev <= mc_hdr->rev) ? 0 : 1))
	->
	if (!0)
	->
	if (1)
		continue;

So basically @new_rev was wrong to use there in the first place. And it
is there since it got committed in 3.13. If anything, it should've been
old_rev FAIK, or

	if (!revision_is_newer(mc_saved_hdr, new_rev))

... whateva...

And to confirm this and so I can stop rubbing my eyes, let's look at the
asm:

 *
 * Returns: The updated number @num_saved of saved microcode patches.
 */
static unsigned int _save_mc(struct microcode_intel **mc_saved,
                             u8 *ucode_ptr, unsigned int num_saved)
{
ffffffff81033f25:       4c 89 65 e0             mov    %r12,-0x20(%rbp)
ffffffff81033f29:       4c 89 6d e8             mov    %r13,-0x18(%rbp)
ffffffff81033f2d:       49 89 f4                mov    %rsi,%r12


ucode_ptr lands in %r12

...

		new_rev	     = mc_hdr->rev;
ffffffff81033f4f:	45 8b 74 24 04       	mov    0x4(%r12),%r14d


new_rev is the second unsigned int in the struct thus new_rev = %r14d = *(%r12 + 4)

...

		if (!revision_is_newer(mc_hdr, new_rev))
ffffffff81033f70:	45 3b 74 24 04       	cmp    0x4(%r12),%r14d
ffffffff81033f75:	73 21                	jae    ffffffff81033f98 <_save_mc+0x88>


So we practically end up doing

	cmpl 0x4(%r12), 0x4(%r12)

and gcc doesn't optimize it away even.


Oh well, let's kill this function completely:


---
From: Borislav Petkov <bp@suse.de>
Date: Fri, 10 Apr 2015 12:50:57 +0200
Subject: [PATCH] x86/microcode/intel: Get rid of revision_is_newer()

It is a one-liner for checking microcode header revisions. On top of
that, it can be used wrong as it was the case in _save_mc(). Get rid of
it.

Signed-off-by: Borislav Petkov <bp@suse.de>
---
 arch/x86/include/asm/microcode_intel.h      | 6 ------
 arch/x86/kernel/cpu/microcode/intel_early.c | 2 +-
 arch/x86/kernel/cpu/microcode/intel_lib.c   | 6 +++---
 3 files changed, 4 insertions(+), 10 deletions(-)

diff --git a/arch/x86/include/asm/microcode_intel.h b/arch/x86/include/asm/microcode_intel.h
index 2b9209c46ca9..a4df6d292228 100644
--- a/arch/x86/include/asm/microcode_intel.h
+++ b/arch/x86/include/asm/microcode_intel.h
@@ -60,12 +60,6 @@ extern int get_matching_microcode(unsigned int csig, int cpf, int rev, void *mc)
 extern int microcode_sanity_check(void *mc, int print_err);
 extern int get_matching_sig(unsigned int csig, int cpf, int rev, void *mc);
 
-static inline int
-revision_is_newer(struct microcode_header_intel *mc_header, int rev)
-{
-	return (mc_header->rev <= rev) ? 0 : 1;
-}
-
 #ifdef CONFIG_MICROCODE_INTEL_EARLY
 extern void __init load_ucode_intel_bsp(void);
 extern void load_ucode_intel_ap(void);
diff --git a/arch/x86/kernel/cpu/microcode/intel_early.c b/arch/x86/kernel/cpu/microcode/intel_early.c
index 98d320c25dff..edae46ebdf32 100644
--- a/arch/x86/kernel/cpu/microcode/intel_early.c
+++ b/arch/x86/kernel/cpu/microcode/intel_early.c
@@ -262,7 +262,7 @@ static unsigned int _save_mc(struct microcode_intel **mc_saved,
 
 		found = 1;
 
-		if (!revision_is_newer(mc_hdr, new_rev))
+		if (mc_hdr->rev <= mc_saved_hdr->rev)
 			continue;
 
 		/*
diff --git a/arch/x86/kernel/cpu/microcode/intel_lib.c b/arch/x86/kernel/cpu/microcode/intel_lib.c
index cd47a510a3f1..63b0a2e059ee 100644
--- a/arch/x86/kernel/cpu/microcode/intel_lib.c
+++ b/arch/x86/kernel/cpu/microcode/intel_lib.c
@@ -154,13 +154,13 @@ int get_matching_sig(unsigned int csig, int cpf, int rev, void *mc)
 /*
  * Returns 1 if update has been found, 0 otherwise.
  */
-int get_matching_microcode(unsigned int csig, int cpf, int rev, void *mc)
+int get_matching_microcode(unsigned int csig, int cpf, int new_rev, void *mc)
 {
 	struct microcode_header_intel *mc_hdr = mc;
 
-	if (!revision_is_newer(mc_hdr, rev))
+	if (mc_hdr->rev <= new_rev)
 		return 0;
 
-	return get_matching_sig(csig, cpf, rev, mc);
+	return get_matching_sig(csig, cpf, new_rev, mc);
 }
 EXPORT_SYMBOL_GPL(get_matching_microcode);
-- 
2.3.5

-- 
Regards/Gruss,
    Boris.

ECO tip #101: Trim your mails when you reply.
--

  reply	other threads:[~2015-04-10 11:14 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-24 10:36 [PATCH 00/13] x86/microcode: Intel early loader cleanups Borislav Petkov
2015-02-24 10:37 ` [PATCH 01/13] x86/microcode/intel: Check if microcode was found before applying Borislav Petkov
2015-02-24 10:37 ` [PATCH 02/13] x86/microcode/intel: Do the mc_saved_src NULL check first Borislav Petkov
2015-02-24 16:20   ` Quentin Casasnovas
2015-02-24 10:37 ` [PATCH 03/13] x86/microcode/intel: Get rid of last arg to load_ucode_intel_bsp() Borislav Petkov
2015-02-24 16:21   ` Quentin Casasnovas
2015-02-24 18:30     ` Borislav Petkov
2015-02-24 10:37 ` [PATCH 04/13] x86/microcode/intel: Simplify load_ucode_intel_bsp() Borislav Petkov
2015-02-24 16:21   ` Quentin Casasnovas
2015-02-24 18:32     ` Borislav Petkov
2015-02-24 10:37 ` [PATCH 05/13] x86/microcode/intel: Make _save_mc() return the updated saved count Borislav Petkov
2015-02-24 16:22   ` Quentin Casasnovas
2015-02-24 10:37 ` [PATCH 06/13] x86/microcode/intel: Sanitize _save_mc() Borislav Petkov
2015-02-24 10:37 ` [PATCH 07/13] x86/microcode/intel: Rename update_match_revision() Borislav Petkov
2015-02-24 16:23   ` Quentin Casasnovas
2015-04-10 11:12     ` Borislav Petkov [this message]
2015-04-10 11:54       ` Quentin Casasnovas
2015-04-10 12:09         ` Borislav Petkov
2015-02-24 10:37 ` [PATCH 08/13] x86/microcode: Consolidate family,model, ... code Borislav Petkov
2015-02-24 16:23   ` Quentin Casasnovas
2015-02-24 10:37 ` [PATCH 09/13] x86/microcode/intel: Simplify generic_load_microcode_early() Borislav Petkov
2015-02-24 10:37 ` [PATCH 10/13] x86/microcode/intel: Move mc arg last in get_matching_{microcode|sig} Borislav Petkov
2015-02-24 16:24   ` Quentin Casasnovas
2015-05-05  9:14     ` Borislav Petkov
2015-02-24 10:37 ` [PATCH 11/13] x86/microcode/intel: Sanitize microcode_pointer() Borislav Petkov
2015-02-24 10:37 ` [PATCH 12/13] x86/microcode/intel: Check scan_microcode()'s retval Borislav Petkov
2015-02-24 10:37 ` [PATCH 13/13] x86/microcode/intel: Fix printing of microcode blobs in show_saved_mc() Borislav Petkov
2015-02-24 16:24   ` Quentin Casasnovas
2015-02-24 16:48     ` Borislav Petkov
2015-02-25  9:41       ` Quentin Casasnovas
2015-02-25 17:55         ` Borislav Petkov
2015-03-03 13:00         ` [tip:x86/microcode] x86/microcode/intel: Fix out of bounds memory access to the extended header tip-bot for Quentin Casasnovas
2015-02-24 16:40 ` [PATCH 00/13] x86/microcode: Intel early loader cleanups Quentin Casasnovas

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150410111218.GC28074@pd.tnic \
    --to=bp@alien8.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=quentin.casasnovas@oracle.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox