From: Sergey Dyasli <sergey.dyasli@citrix.com>
To: Chao Gao <chao.gao@intel.com>
Cc: "sergey.dyasli@citrix.com >> Sergey Dyasli"
<sergey.dyasli@citrix.com>, "Ashok Raj" <ashok.raj@intel.com>,
"Wei Liu" <wl@xen.org>,
"Andrew Cooper" <andrew.cooper3@citrix.com>,
"Jan Beulich" <jbeulich@suse.com>,
xen-devel@lists.xenproject.org,
"Roger Pau Monné" <roger.pau@citrix.com>
Subject: Re: [Xen-devel] [PATCH v9 15/15] microcode: block #NMI handling when loading an ucode
Date: Wed, 28 Aug 2019 09:52:00 +0100 [thread overview]
Message-ID: <5b22b82b-e3cc-3ead-46f2-624ff9e5d2d7@citrix.com> (raw)
In-Reply-To: <20190827045159.GA28509@gao-cwp>
On 27/08/2019 05:52, Chao Gao wrote:
> On Mon, Aug 26, 2019 at 04:07:59PM +0800, Chao Gao wrote:
>> On Fri, Aug 23, 2019 at 09:46:37AM +0100, Sergey Dyasli wrote:
>>> On 19/08/2019 02:25, Chao Gao wrote:
>>>> register an nmi callback. And this callback does busy-loop on threads
>>>> which are waiting for loading completion. Control threads send NMI to
>>>> slave threads to prevent NMI acceptance during ucode loading.
>>>>
>>>> Signed-off-by: Chao Gao <chao.gao@intel.com>
>>>> ---
>>>> Changes in v9:
>>>> - control threads send NMI to all other threads. Slave threads will
>>>> stay in the NMI handling to prevent NMI acceptance during ucode
>>>> loading. Note that self-nmi is invalid according to SDM.
>>>
>>> To me this looks like a half-measure: why keep only slave threads in
>>> the NMI handler, when master threads can update the microcode from
>>> inside the NMI handler as well?
>>
>> No special reason. Because the issue we want to address is that slave
>> threads might go to handle NMI and access MSRs when master thread is
>> loading ucode. So we only keep slave threads in the NMI handler.
>>
>>>
>>> You mention that self-nmi is invalid, but Xen has self_nmi() which is
>>> used for apply_alternatives() during boot, so can be trusted to work.
>>
>> Sorry, I meant using self shorthand to send self-nmi. I tried to use
>> self shorthand but got APIC error. And I agree that it is better to
>> make slave thread call self_nmi() itself.
>>
>>>
>>> I experimented a bit with the following approach: after loading_state
>>> becomes LOADING_CALLIN, each cpu issues a self_nmi() and rendezvous
>>> via cpu_callin_map into LOADING_ENTER to do a ucode update directly in
>>> the NMI handler. And it seems to work.
>>>
>>> Separate question is about the safety of this approach: can we be sure
>>> that a ucode update would not reset the status of the NMI latch? I.e.
>>> can it cause another NMI to be delivered while Xen already handles one?
>>
>> Ashok, what's your opinion on Sergey's approach and his concern?
>
> Hi Sergey,
>
> I talked with Ashok. We think your approach is better. I will follow
> your approach in v10. It would be much helpful if you post your patch
> so that I can just rebase it onto other patches.
Sure thing. The below code is my first attempt at improving the original
patch. It can benefit from some further refactoring.
---
xen/arch/x86/microcode.c | 108 ++++++++++++++++++++++++++++-----------
1 file changed, 79 insertions(+), 29 deletions(-)
diff --git a/xen/arch/x86/microcode.c b/xen/arch/x86/microcode.c
index 91f9e811f8..ba2363406f 100644
--- a/xen/arch/x86/microcode.c
+++ b/xen/arch/x86/microcode.c
@@ -36,8 +36,10 @@
#include <xen/earlycpio.h>
#include <xen/watchdog.h>
+#include <asm/apic.h>
#include <asm/delay.h>
#include <asm/msr.h>
+#include <asm/nmi.h>
#include <asm/processor.h>
#include <asm/setup.h>
#include <asm/microcode.h>
@@ -232,6 +234,7 @@ DEFINE_PER_CPU(struct cpu_signature, cpu_sig);
*/
static cpumask_t cpu_callin_map;
static atomic_t cpu_out, cpu_updated;
+struct microcode_patch *nmi_patch;
/*
* Return a patch that covers current CPU. If there are multiple patches,
@@ -337,15 +340,25 @@ static int microcode_update_cpu(const struct microcode_patch *patch)
return err;
}
+static void slave_thread_work(void)
+{
+ /* Do nothing, just wait */
+ while ( loading_state != LOADING_EXIT )
+ cpu_relax();
+}
+
static int slave_thread_fn(void)
{
- unsigned int cpu = smp_processor_id();
unsigned int master = cpumask_first(this_cpu(cpu_sibling_mask));
while ( loading_state != LOADING_CALLIN )
+ {
+ if ( loading_state == LOADING_EXIT )
+ return 0;
cpu_relax();
+ }
- cpumask_set_cpu(cpu, &cpu_callin_map);
+ self_nmi();
while ( loading_state != LOADING_EXIT )
cpu_relax();
@@ -356,30 +369,35 @@ static int slave_thread_fn(void)
return 0;
}
-static int master_thread_fn(const struct microcode_patch *patch)
+static void master_thread_work(void)
{
- unsigned int cpu = smp_processor_id();
- int ret = 0;
-
- while ( loading_state != LOADING_CALLIN )
- cpu_relax();
-
- cpumask_set_cpu(cpu, &cpu_callin_map);
+ int ret;
while ( loading_state != LOADING_ENTER )
+ {
+ if ( loading_state == LOADING_EXIT )
+ return;
cpu_relax();
+ }
- /*
- * If an error happened, control thread would set 'loading_state'
- * to LOADING_EXIT. Don't perform ucode loading for this case
- */
- if ( loading_state == LOADING_EXIT )
- return ret;
-
- ret = microcode_ops->apply_microcode(patch);
+ ret = microcode_ops->apply_microcode(nmi_patch);
if ( !ret )
atomic_inc(&cpu_updated);
atomic_inc(&cpu_out);
+}
+
+static int master_thread_fn(const struct microcode_patch *patch)
+{
+ int ret = 0;
+
+ while ( loading_state != LOADING_CALLIN )
+ {
+ if ( loading_state == LOADING_EXIT )
+ return ret;
+ cpu_relax();
+ }
+
+ self_nmi();
while ( loading_state != LOADING_EXIT )
cpu_relax();
@@ -387,35 +405,40 @@ static int master_thread_fn(const struct microcode_patch *patch)
return ret;
}
-static int control_thread_fn(const struct microcode_patch *patch)
+static void control_thread_work(void)
{
- unsigned int cpu = smp_processor_id(), done;
- unsigned long tick;
int ret;
- /* Allow threads to call in */
- loading_state = LOADING_CALLIN;
- smp_mb();
-
- cpumask_set_cpu(cpu, &cpu_callin_map);
-
/* Waiting for all threads calling in */
ret = wait_for_condition(wait_cpu_callin,
(void *)(unsigned long)num_online_cpus(),
MICROCODE_CALLIN_TIMEOUT_US);
if ( ret ) {
loading_state = LOADING_EXIT;
- return ret;
+ return;
}
/* Let master threads load the given ucode update */
loading_state = LOADING_ENTER;
smp_mb();
- ret = microcode_ops->apply_microcode(patch);
+ ret = microcode_ops->apply_microcode(nmi_patch);
if ( !ret )
atomic_inc(&cpu_updated);
atomic_inc(&cpu_out);
+}
+
+static int control_thread_fn(const struct microcode_patch *patch)
+{
+ unsigned int done;
+ unsigned long tick;
+ int ret;
+
+ /* Allow threads to call in */
+ loading_state = LOADING_CALLIN;
+ smp_mb();
+
+ self_nmi();
tick = rdtsc_ordered();
/* Waiting for master threads finishing update */
@@ -481,12 +504,35 @@ static int do_microcode_update(void *patch)
return ret;
}
+static int microcode_nmi_callback(const struct cpu_user_regs *regs, int cpu)
+{
+ unsigned int master = cpumask_first(this_cpu(cpu_sibling_mask));
+ unsigned int controller = cpumask_first(&cpu_online_map);
+
+ /* System-generated NMI, will be ignored */
+ if ( loading_state == LOADING_PREPARE )
+ return 0;
+
+ ASSERT(loading_state == LOADING_CALLIN);
+ cpumask_set_cpu(cpu, &cpu_callin_map);
+
+ if ( cpu == controller )
+ control_thread_work();
+ else if ( cpu == master )
+ master_thread_work();
+ else
+ slave_thread_work();
+
+ return 0;
+}
+
int microcode_update(XEN_GUEST_HANDLE_PARAM(const_void) buf, unsigned long len)
{
int ret;
void *buffer;
unsigned int cpu, updated;
struct microcode_patch *patch;
+ nmi_callback_t *saved_nmi_callback;
if ( len != (uint32_t)len )
return -E2BIG;
@@ -551,6 +597,9 @@ int microcode_update(XEN_GUEST_HANDLE_PARAM(const_void) buf, unsigned long len)
* watchdog timeout.
*/
watchdog_disable();
+
+ nmi_patch = patch;
+ saved_nmi_callback = set_nmi_callback(microcode_nmi_callback);
/*
* Late loading dance. Why the heavy-handed stop_machine effort?
*
@@ -563,6 +612,7 @@ int microcode_update(XEN_GUEST_HANDLE_PARAM(const_void) buf, unsigned long len)
* conservative and good.
*/
ret = stop_machine_run(do_microcode_update, patch, NR_CPUS);
+ set_nmi_callback(saved_nmi_callback);
watchdog_enable();
updated = atomic_read(&cpu_updated);
--
2.17.1
_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xenproject.org
https://lists.xenproject.org/mailman/listinfo/xen-devel
next prev parent reply other threads:[~2019-08-28 8:52 UTC|newest]
Thread overview: 57+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-19 1:25 [Xen-devel] [PATCH v9 00/15] improve late microcode loading Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 01/15] microcode/intel: extend microcode_update_match() Chao Gao
2019-08-28 15:12 ` Jan Beulich
2019-08-29 7:15 ` Chao Gao
2019-08-29 7:14 ` Jan Beulich
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 02/15] microcode/amd: fix memory leak Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 03/15] microcode/amd: distinguish old and mismatched ucode in microcode_fits() Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 04/15] microcode: introduce a global cache of ucode patch Chao Gao
2019-08-22 11:11 ` Roger Pau Monné
2019-08-28 15:21 ` Jan Beulich
2019-08-29 10:18 ` Jan Beulich
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 05/15] microcode: clean up microcode_resume_cpu Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 06/15] microcode: remove struct ucode_cpu_info Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 07/15] microcode: remove pointless 'cpu' parameter Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 08/15] microcode/amd: call svm_host_osvw_init() in common code Chao Gao
2019-08-22 13:08 ` Roger Pau Monné
2019-08-28 15:26 ` Jan Beulich
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 09/15] microcode: pass a patch pointer to apply_microcode() Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 10/15] microcode: split out apply_microcode() from cpu_request_microcode() Chao Gao
2019-08-22 13:59 ` Roger Pau Monné
2019-08-29 10:06 ` Jan Beulich
2019-08-30 3:22 ` Chao Gao
2019-08-30 7:25 ` Jan Beulich
2019-08-29 10:19 ` Jan Beulich
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 11/15] microcode: unify loading update during CPU resuming and AP wakeup Chao Gao
2019-08-22 14:10 ` Roger Pau Monné
2019-08-22 16:44 ` Chao Gao
2019-08-23 9:09 ` Roger Pau Monné
2019-08-29 7:37 ` Chao Gao
2019-08-29 8:16 ` Roger Pau Monné
2019-08-29 10:26 ` Jan Beulich
2019-08-29 10:29 ` Jan Beulich
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 12/15] microcode: reduce memory allocation and copy when creating a patch Chao Gao
2019-08-23 8:11 ` Roger Pau Monné
2019-08-26 7:03 ` Chao Gao
2019-08-26 8:11 ` Roger Pau Monné
2019-08-29 10:47 ` Jan Beulich
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 13/15] x86/microcode: Synchronize late microcode loading Chao Gao
2019-08-19 10:27 ` Sergey Dyasli
2019-08-19 14:49 ` Chao Gao
2019-08-29 12:06 ` Jan Beulich
2019-08-30 3:30 ` Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 14/15] microcode: remove microcode_update_lock Chao Gao
2019-08-19 1:25 ` [Xen-devel] [PATCH v9 15/15] microcode: block #NMI handling when loading an ucode Chao Gao
2019-08-23 8:46 ` Sergey Dyasli
2019-08-26 8:07 ` Chao Gao
2019-08-27 4:52 ` Chao Gao
2019-08-28 8:52 ` Sergey Dyasli [this message]
2019-08-29 12:11 ` Jan Beulich
2019-08-30 6:35 ` Chao Gao
2019-09-09 5:52 ` Chao Gao
2019-09-09 6:16 ` Jan Beulich
2019-08-29 12:22 ` Jan Beulich
2019-08-30 6:33 ` Chao Gao
2019-08-30 7:30 ` Jan Beulich
2019-08-22 7:51 ` [Xen-devel] [PATCH v9 00/15] improve late microcode loading Sergey Dyasli
2019-08-22 15:39 ` Chao Gao
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=5b22b82b-e3cc-3ead-46f2-624ff9e5d2d7@citrix.com \
--to=sergey.dyasli@citrix.com \
--cc=andrew.cooper3@citrix.com \
--cc=ashok.raj@intel.com \
--cc=chao.gao@intel.com \
--cc=jbeulich@suse.com \
--cc=roger.pau@citrix.com \
--cc=wl@xen.org \
--cc=xen-devel@lists.xenproject.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;
as well as URLs for NNTP newsgroup(s).