linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xen/x86: Update topology map for PV VCPUs
@ 2016-10-05 17:09 Boris Ostrovsky
  2016-10-05 17:41 ` Andrew Cooper
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Boris Ostrovsky @ 2016-10-05 17:09 UTC (permalink / raw)
  To: david.vrabel, jgross
  Cc: xen-devel, linux-kernel, andrew.cooper3, Boris Ostrovsky, stable

Early during boot topology_update_package_map() computes
logical_pkg_ids for all present processors.

Later, when processors are brought up, identify_cpu() updates
these values based on phys_pkg_id which is a function of
initial_apicid. On PV guests the latter may point to a
non-existing node, causing logical_pkg_ids to be set to -1.

Intel's RAPL uses logical_pkg_id (as topology_logical_package_id())
to index its arrays and therefore in this case will point to index
65535 (since logical_pkg_id is a u16). This could lead to either a
crash or may actually access random memory location.

As a workaround, we recompute topology during CPU bringup to reset
logical_pkg_id to a valid value.

(The reason for initial_apicid being bogus is because it is
initial_apicid of the processor from which the guest is launched.
This value is CPUID(1).EBX[31:24])

Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
Cc: stable@vger.kernel.org
---

Copying Andrew for the CPUID part.

 arch/x86/xen/smp.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/x86/xen/smp.c b/arch/x86/xen/smp.c
index 311acad..9fa27ce 100644
--- a/arch/x86/xen/smp.c
+++ b/arch/x86/xen/smp.c
@@ -87,6 +87,12 @@ static void cpu_bringup(void)
 	cpu_data(cpu).x86_max_cores = 1;
 	set_cpu_sibling_map(cpu);
 
+	/*
+	 * identify_cpu() may have set logical_pkg_id to -1 due
+	 * to incorrect phys_proc_id. Let's re-comupte it.
+	 */
+	topology_update_package_map(apic->cpu_present_to_apicid(cpu), cpu);
+
 	xen_setup_cpu_clockevents();
 
 	notify_cpu_starting(cpu);
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] xen/x86: Update topology map for PV VCPUs
  2016-10-05 17:09 [PATCH] xen/x86: Update topology map for PV VCPUs Boris Ostrovsky
@ 2016-10-05 17:41 ` Andrew Cooper
  2016-10-06 12:14 ` [Xen-devel] " Jan Beulich
  2016-10-06 14:12 ` David Vrabel
  2 siblings, 0 replies; 4+ messages in thread
From: Andrew Cooper @ 2016-10-05 17:41 UTC (permalink / raw)
  To: Boris Ostrovsky, david.vrabel, jgross; +Cc: xen-devel, linux-kernel, stable

On 05/10/16 18:09, Boris Ostrovsky wrote:
> Early during boot topology_update_package_map() computes
> logical_pkg_ids for all present processors.
>
> Later, when processors are brought up, identify_cpu() updates
> these values based on phys_pkg_id which is a function of
> initial_apicid. On PV guests the latter may point to a
> non-existing node, causing logical_pkg_ids to be set to -1.
>
> Intel's RAPL uses logical_pkg_id (as topology_logical_package_id())
> to index its arrays and therefore in this case will point to index
> 65535 (since logical_pkg_id is a u16). This could lead to either a
> crash or may actually access random memory location.
>
> As a workaround, we recompute topology during CPU bringup to reset
> logical_pkg_id to a valid value.
>
> (The reason for initial_apicid being bogus is because it is
> initial_apicid of the processor from which the guest is launched.
> This value is CPUID(1).EBX[31:24])
>
> Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
> Cc: stable@vger.kernel.org
> ---
>
> Copying Andrew for the CPUID part.

Yeah - that leaf is usually fiction.  (Specifically, the fiction of
whichever cpu a specific toolstack function happened to sample at the
point in time that it was choosing which cpuid values to fake up for the
guest).

I am currently working on fixing the reported topology information to be
architecturally plausible, but current and previous hypervisors will be
wrong.

~Andrew

>
>  arch/x86/xen/smp.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/arch/x86/xen/smp.c b/arch/x86/xen/smp.c
> index 311acad..9fa27ce 100644
> --- a/arch/x86/xen/smp.c
> +++ b/arch/x86/xen/smp.c
> @@ -87,6 +87,12 @@ static void cpu_bringup(void)
>  	cpu_data(cpu).x86_max_cores = 1;
>  	set_cpu_sibling_map(cpu);
>  
> +	/*
> +	 * identify_cpu() may have set logical_pkg_id to -1 due
> +	 * to incorrect phys_proc_id. Let's re-comupte it.
> +	 */
> +	topology_update_package_map(apic->cpu_present_to_apicid(cpu), cpu);
> +
>  	xen_setup_cpu_clockevents();
>  
>  	notify_cpu_starting(cpu);

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Xen-devel] [PATCH] xen/x86: Update topology map for PV VCPUs
  2016-10-05 17:09 [PATCH] xen/x86: Update topology map for PV VCPUs Boris Ostrovsky
  2016-10-05 17:41 ` Andrew Cooper
@ 2016-10-06 12:14 ` Jan Beulich
  2016-10-06 14:12 ` David Vrabel
  2 siblings, 0 replies; 4+ messages in thread
From: Jan Beulich @ 2016-10-06 12:14 UTC (permalink / raw)
  To: Boris Ostrovsky
  Cc: andrew.cooper3, david.vrabel, xen-devel, Juergen Gross,
	linux-kernel, stable

>>> On 05.10.16 at 19:09, <boris.ostrovsky@oracle.com> wrote:
> Early during boot topology_update_package_map() computes
> logical_pkg_ids for all present processors.
> 
> Later, when processors are brought up, identify_cpu() updates
> these values based on phys_pkg_id which is a function of
> initial_apicid. On PV guests the latter may point to a
> non-existing node, causing logical_pkg_ids to be set to -1.
> 
> Intel's RAPL uses logical_pkg_id (as topology_logical_package_id())
> to index its arrays and therefore in this case will point to index
> 65535 (since logical_pkg_id is a u16). This could lead to either a
> crash or may actually access random memory location.

Another clear indication that such fields should never be touched
(and hence consumers either be fixed or disabled) when running as
PV guest under Xen.

Jan

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Xen-devel] [PATCH] xen/x86: Update topology map for PV VCPUs
  2016-10-05 17:09 [PATCH] xen/x86: Update topology map for PV VCPUs Boris Ostrovsky
  2016-10-05 17:41 ` Andrew Cooper
  2016-10-06 12:14 ` [Xen-devel] " Jan Beulich
@ 2016-10-06 14:12 ` David Vrabel
  2 siblings, 0 replies; 4+ messages in thread
From: David Vrabel @ 2016-10-06 14:12 UTC (permalink / raw)
  To: Boris Ostrovsky, david.vrabel, jgross
  Cc: xen-devel, linux-kernel, stable, andrew.cooper3

On 05/10/16 18:09, Boris Ostrovsky wrote:
> Early during boot topology_update_package_map() computes
> logical_pkg_ids for all present processors.
> 
> Later, when processors are brought up, identify_cpu() updates
> these values based on phys_pkg_id which is a function of
> initial_apicid. On PV guests the latter may point to a
> non-existing node, causing logical_pkg_ids to be set to -1.
> 
> Intel's RAPL uses logical_pkg_id (as topology_logical_package_id())
> to index its arrays and therefore in this case will point to index
> 65535 (since logical_pkg_id is a u16). This could lead to either a
> crash or may actually access random memory location.
> 
> As a workaround, we recompute topology during CPU bringup to reset
> logical_pkg_id to a valid value.
> 
> (The reason for initial_apicid being bogus is because it is
> initial_apicid of the processor from which the guest is launched.
> This value is CPUID(1).EBX[31:24])

Applied to for-linus-4.9, thanks.

David

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2016-10-06 14:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-05 17:09 [PATCH] xen/x86: Update topology map for PV VCPUs Boris Ostrovsky
2016-10-05 17:41 ` Andrew Cooper
2016-10-06 12:14 ` [Xen-devel] " Jan Beulich
2016-10-06 14:12 ` David Vrabel

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).