All of lore.kernel.org
 help / color / mirror / Atom feed
From: Borislav Petkov <borislav.petkov@amd.com>
To: Peter Zijlstra <peterz@infradead.org>
Cc: Ingo Molnar <mingo@kernel.org>,
	Stephane Eranian <eranian@google.com>,
	<linux-kernel@vger.kernel.org>, <andi@firstfloor.org>,
	<mingo@elte.hu>, <ming.m.lin@intel.com>,
	Andreas Herrmann <andreas.herrmann3@amd.com>,
	Borislav Petkov <borislav.petkov@amd.com>,
	Dimitri Sivanich <sivanich@sgi.com>,
	Dmitry Adamushko <dmitry.adamushko@gmail.com>
Subject: Re: [PATCH] perf/x86: check ucode before disabling PEBS on SandyBridge
Date: Fri, 8 Jun 2012 15:51:17 +0200	[thread overview]
Message-ID: <20120608135117.GB31359@aftab.osrc.amd.com> (raw)
In-Reply-To: <1339161972.2507.13.camel@laptop>

On Fri, Jun 08, 2012 at 03:26:12PM +0200, Peter Zijlstra wrote:
> On Fri, 2012-06-08 at 12:00 +0200, Peter Zijlstra wrote:
> > > > Or we could put a hook in the ucode loader.
> > > 
> > > I'd really suggest the latter - I doubt this will be our only 
> > > ucode dependent quirk, going forward ...
> > 
> > Yeah, am in the middle of writing that.. 
> 
> OK so the micro-code stuff is a complete trainwreck.
> 
> The very worst is that it does per-cpu micro-code updates, not machine
> wide. This results in it being able to have different revisions on
> different cpus. This in turn makes the below O(n^2) :/

Reportedly, there are some obscure systems which need different
microcode versions per CPU:

http://lkml.indiana.edu/hypermail/linux/kernel/1105.3/01010.html

> The biggest problem is finding when the minimum revision changes, at
> best this is a n log n sorting problem due to the per-cpu setup, but I
> couldn't be arsed to implement a tree or anything fancy since it all
> stinks anyway.

I know. Can't you just iterate over all CPUs and collect the lowest
ucode version? Provided, of course, newer microcode versions means a
higher version number.

> Why do we have per-cpu firmware anyway?

See above.

[ … ]

> diff --git a/arch/x86/kernel/cpu/perf_event_intel.c b/arch/x86/kernel/cpu/perf_event_intel.c
> index 5ec146c..bde86cf 100644
> --- a/arch/x86/kernel/cpu/perf_event_intel.c
> +++ b/arch/x86/kernel/cpu/perf_event_intel.c
> @@ -15,6 +15,7 @@
>  
>  #include <asm/hardirq.h>
>  #include <asm/apic.h>
> +#include <asm/microcode.h>
>  
>  #include "perf_event.h"
>  
> @@ -1714,11 +1715,67 @@ static __init void intel_clovertown_quirk(void)
>  	x86_pmu.pebs_constraints = NULL;
>  }
>  
> +static const u32 snb_ucode_rev = 0x28;
> +
> +static void intel_snb_verify_ucode(void)
> +{
> +	u32 rev = UINT_MAX;
> +	int pebs_broken = 0;
> +	int cpu;
> +
> +	get_online_cpus();
> +	/*
> +	 * Because the microcode loader is bloody stupid and allows different
> +	 * revisions per cpu and does strictly per-cpu loading, we now have to
> +	 * check all cpus to determine the minimally installed revision.
> +	 *
> +	 * This makes updating the microcode O(n^2) in the number of CPUs :/
> +	 */
> +	for_each_online_cpu(cpu)
> +		rev = min(cpu_data(cpu).microcode, rev);
> +	put_online_cpus();
> +
> +	pebs_broken = (rev < snb_ucode_rev);
> +
> +	if (pebs_broken == x86_pmu.pebs_broken)
> +		return;
> +
> +	/*
> +	 * Serialized by the microcode lock..
> +	 */
> +	if (x86_pmu.pebs_broken) {
> +		pr_info("PEBS enabled due to micro-code update\n");
> +		x86_pmu.pebs_broken = 0;
> +	} else {
> +		pr_info("PEBS disabled due to CPU errata, "
> +			"please upgrade micro-code to at least %x (current: %x)\n",
> +			snb_ucode_rev, rev);
> +		x86_pmu.pebs_broken = 1;
> +	}
> +}
> +
> +static int intel_snb_ucode_notifier(struct notifier_block *self,
> +				   unsigned long action, void *_uci)
> +{
> +	/*
> +	 * Since ucode cannot be down-graded, and no future ucode revision
> +	 * is known to break PEBS again, we're ok with MICROCODE_CAN_UPDATE.
> +	 */
> +
> +	if (action == MICROCODE_UPDATED)
> +		intel_snb_verify_ucode();

Actually, the deal here is that you could have received microcode
already from BIOS and you won't have to necessarily load ucode from
userspace with the Linux ucode loader.

Which means that on boxes which already come with PEBS-good microcode
version from the BIOS, you don't need the ucode notifier because you
most certainly are not loading newer ucode version from userspace - the
newest version is in the BIOS already.

In these cases, you already have PEBS fixed but since you're not loading
any ucode, you won't run intel_snb_verify_ucode().

So, you want to check for PEBS twice (and for all other features fixed
by ucode and tested for earlier than the ucode loader, for that matter):

* once when perf inits

* twice, a bit later when the ucode loader has loaded something from
userspace and the notifier corrects it.

Btw, this is why we wanted to load ucode as early as possible but
there's no progress on the whole thing right now...

-- 
Regards/Gruss,
Boris.

Advanced Micro Devices GmbH
Einsteinring 24, 85609 Dornach
GM: Alberto Bozzo
Reg: Dornach, Landkreis Muenchen
HRB Nr. 43632 WEEE Registernr: 129 19551


  reply	other threads:[~2012-06-08 13:51 UTC|newest]

Thread overview: 107+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-07  7:15 [PATCH] perf/x86: check ucode before disabling PEBS on SandyBridge Stephane Eranian
2012-06-07 10:18 ` Peter Zijlstra
2012-06-07 10:35   ` Stephane Eranian
2012-06-07 10:45     ` Peter Zijlstra
2012-06-07 10:48       ` Stephane Eranian
2012-06-07 11:15         ` Peter Zijlstra
2012-06-08  9:35           ` Ingo Molnar
2012-06-08 10:00             ` Peter Zijlstra
2012-06-08 10:03               ` Stephane Eranian
2012-06-08 13:26               ` Peter Zijlstra
2012-06-08 13:51                 ` Borislav Petkov [this message]
2012-06-08 13:54                   ` Peter Zijlstra
2012-06-08 14:15                     ` Borislav Petkov
2012-06-08 14:20                       ` Peter Zijlstra
2012-06-08 14:36                         ` Borislav Petkov
2012-06-08 14:45                           ` Peter Zijlstra
2012-06-08 15:09                             ` Borislav Petkov
2012-06-08 13:56                   ` Peter Zijlstra
2012-06-08 14:03                     ` Borislav Petkov
2012-06-08 13:59                   ` Peter Zijlstra
2012-06-08 14:03                     ` Stephane Eranian
2012-06-08 14:17                       ` Borislav Petkov
2012-06-08 16:02                         ` H. Peter Anvin
2012-06-08 21:02                   ` Henrique de Moraes Holschuh
2012-06-08 21:16                     ` Borislav Petkov
2012-06-08 14:07                 ` Stephane Eranian
2012-06-08 14:13                   ` Peter Zijlstra
2012-06-08 14:19                   ` Borislav Petkov
2012-06-08 14:23                   ` Peter Zijlstra
2012-06-08 14:26                     ` Stephane Eranian
2012-06-08 14:25                   ` Peter Zijlstra
2012-06-08 14:26                     ` Stephane Eranian
2012-06-12  8:14                     ` Stephane Eranian
2012-06-08 16:28                 ` Stephane Eranian
2012-06-08 18:49                   ` Peter Zijlstra
2012-06-08 16:50                 ` Andi Kleen
2012-06-08 18:05                 ` Borislav Petkov
2012-06-08 18:52                   ` Peter Zijlstra
2012-06-08 20:38                     ` Borislav Petkov
2012-06-12 17:07                 ` Robert Richter
2012-06-12 17:09                   ` Stephane Eranian
2012-06-12 17:13                     ` Peter Zijlstra
2012-06-12 17:17                       ` Borislav Petkov
2012-06-12 17:18                         ` Peter Zijlstra
2012-06-12 17:23                           ` Borislav Petkov
2012-06-12 17:26                             ` Peter Zijlstra
2012-06-12 17:35                               ` Borislav Petkov
2012-06-12 17:40                                 ` Peter Zijlstra
2012-06-12 18:04                                   ` Andi Kleen
2012-06-12 20:10                                   ` Borislav Petkov
2012-06-13  1:04                                 ` Henrique de Moraes Holschuh
2012-06-13  6:51                                   ` Borislav Petkov
2012-06-13 12:36                                     ` Henrique de Moraes Holschuh
2012-06-13 16:11                                       ` Borislav Petkov
2012-06-13 21:41                                         ` Henrique de Moraes Holschuh
2012-06-15 12:37                                         ` Borislav Petkov
2012-06-15 12:42                                           ` Peter Zijlstra
2012-06-15 12:52                                             ` Borislav Petkov
2012-06-15 16:52                                               ` [PATCH] x86, microcode: Make reload interface per system Borislav Petkov
2012-06-15 17:16                                                 ` Peter Zijlstra
2012-06-15 17:49                                                   ` Borislav Petkov
2012-06-19  2:46                                                 ` Henrique de Moraes Holschuh
2012-06-19  3:31                                                   ` H. Peter Anvin
2012-06-19  5:11                                                     ` Borislav Petkov
2012-06-19  8:17                                                       ` Peter Zijlstra
2012-06-19 10:22                                                         ` Borislav Petkov
2012-06-19 10:26                                                           ` Peter Zijlstra
2012-06-19 15:06                                                             ` Borislav Petkov
2012-06-19 15:57                                                               ` H. Peter Anvin
2012-06-19 18:22                                                             ` Henrique de Moraes Holschuh
2012-06-19 20:22                                                               ` H. Peter Anvin
2012-06-20 23:46                                                                 ` Henrique de Moraes Holschuh
2012-06-20 23:49                                                                   ` H. Peter Anvin
2012-06-21  0:06                                                                     ` Henrique de Moraes Holschuh
2012-06-21  0:18                                                                       ` H. Peter Anvin
2012-06-21  2:33                                                                         ` Henrique de Moraes Holschuh
2012-06-19  5:03                                                   ` Borislav Petkov
2012-06-19 18:57                                                     ` Henrique de Moraes Holschuh
2012-06-19 22:19                                                       ` Borislav Petkov
2012-06-19  2:42                                           ` [PATCH] perf/x86: check ucode before disabling PEBS on SandyBridge Henrique de Moraes Holschuh
2012-06-12 17:23                       ` Robert Richter
2012-06-12 17:15                     ` Borislav Petkov
2012-06-07 13:28   ` Andi Kleen
2012-06-07 13:27 ` Andi Kleen
2012-06-12 18:35 ` H. Peter Anvin
2012-06-12 19:07   ` Borislav Petkov
2012-06-12 19:33     ` Peter Zijlstra
2012-06-12 19:35       ` Andi Kleen
2012-06-12 19:49       ` Borislav Petkov
2012-06-12 20:28         ` H. Peter Anvin
2012-06-12 20:37           ` Borislav Petkov
2012-06-12 20:42             ` H. Peter Anvin
2012-06-12 20:56               ` Borislav Petkov
2012-06-12 20:58                 ` H. Peter Anvin
2012-06-12 21:04                   ` Borislav Petkov
2012-06-13  8:38                   ` Peter Zijlstra
2012-06-13 14:00                     ` H. Peter Anvin
2012-06-13 15:37                       ` Peter Zijlstra
2012-06-13 15:46                         ` H. Peter Anvin
2012-06-13 12:39               ` Peter Zijlstra
2012-06-13 13:22                 ` H. Peter Anvin
2012-06-13 14:01                 ` Henrique de Moraes Holschuh
2012-06-13  8:32         ` Peter Zijlstra
2012-06-13 13:59           ` H. Peter Anvin
2012-06-13 15:32             ` Peter Zijlstra
2012-06-13 15:38               ` H. Peter Anvin
2012-06-12 20:27       ` H. Peter Anvin

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=20120608135117.GB31359@aftab.osrc.amd.com \
    --to=borislav.petkov@amd.com \
    --cc=andi@firstfloor.org \
    --cc=andreas.herrmann3@amd.com \
    --cc=dmitry.adamushko@gmail.com \
    --cc=eranian@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ming.m.lin@intel.com \
    --cc=mingo@elte.hu \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sivanich@sgi.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.