public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH] x86: stop machine text poke should issue sync core
@ 2011-02-28 15:24 Mathieu Desnoyers
  2011-03-03  5:27 ` Masami Hiramatsu
  2011-03-03  6:10 ` H. Peter Anvin
  0 siblings, 2 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2011-02-28 15:24 UTC (permalink / raw)
  To: Peter Zijlstra, Arjan van de Ven, H. Peter Anvin,
	Masami Hiramatsu
  Cc: Thomas Gleixner, Steven Rostedt, Ingo Molnar, Andrew Morton,
	Andi Kleen, Frederic Weisbecker, linux-kernel

Intel Archiecture Software Developer's Manual section 7.1.3 specifies that a
core serializing instruction such as "cpuid" should be executed on _each_ core
before the new instruction is made visible.

Failure to do so can lead to unspecified behavior (Intel XMC erratas include
General Protection Fault in the list), so we should avoid this at all cost.

This problem can affect modified code executed by interrupt handlers after
interrupt are re-enabled at the end of stop_machine, because no core serializing
instruction is executed between the code modification and the moment interrupts
are reenabled.

Because stop_machine_text_poke performs the text modification from the first CPU
decrementing stop_machine_first, modified code executed in thread context is
also affected by this problem. To explain why, we have to split the CPUs in two
categories: the CPU that initiates the text modification (calls text_poke_smp)
and all the others. The scheduler, executed on all other CPUs after
stop_machine, issues an "iret" core serializing instruction, and therefore
handles core serialization for all these CPUs. However, the text modification
initiator can continue its execution on the same thread and access the modified
text without any scheduler call. Given that the CPU that initiates the code
modification is not guaranteed to be the one actually performing the code
modification, it falls into the XMC errata.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
CC: Peter Zijlstra <peterz@infradead.org>
CC: Arjan van de Ven <arjan@infradead.org>
CC: "H. Peter Anvin" <hpa@zytor.com>
CC: Thomas Gleixner <tglx@linutronix.de>
CC: Steven Rostedt <rostedt@goodmis.org>
CC: Ingo Molnar <mingo@elte.hu>
CC: Andrew Morton <akpm@linux-foundation.org>
CC: Andi Kleen <andi@firstfloor.org>
CC: Frederic Weisbecker <fweisbec@gmail.com>
CC: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
---
 arch/x86/kernel/alternative.c |    6 ++++++
 1 file changed, 6 insertions(+)

Index: linux-2.6-lttng/arch/x86/kernel/alternative.c
===================================================================
--- linux-2.6-lttng.orig/arch/x86/kernel/alternative.c
+++ linux-2.6-lttng/arch/x86/kernel/alternative.c
@@ -612,6 +612,12 @@ static int __kprobes stop_machine_text_p
 
 	flush_icache_range((unsigned long)tpp->addr,
 			   (unsigned long)tpp->addr + tpp->len);
+	/*
+	 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
+	 * that a core serializing instruction such as "cpuid" should be
+	 * executed on _each_ core before the new instruction is made visible.
+	 */
+	sync_core();
 	return 0;
 }
 
-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

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

* Re: [RFC PATCH] x86: stop machine text poke should issue sync core
  2011-02-28 15:24 [RFC PATCH] x86: stop machine text poke should issue sync core Mathieu Desnoyers
@ 2011-03-03  5:27 ` Masami Hiramatsu
  2011-03-03  6:10 ` H. Peter Anvin
  1 sibling, 0 replies; 5+ messages in thread
From: Masami Hiramatsu @ 2011-03-03  5:27 UTC (permalink / raw)
  To: Mathieu Desnoyers
  Cc: Peter Zijlstra, Arjan van de Ven, H. Peter Anvin, Thomas Gleixner,
	Steven Rostedt, Ingo Molnar, Andrew Morton, Andi Kleen,
	Frederic Weisbecker, linux-kernel,
	2nddept-manager@sdl.hitachi.co.jp

(2011/03/01 0:24), Mathieu Desnoyers wrote:
> Intel Archiecture Software Developer's Manual section 7.1.3 specifies that a
> core serializing instruction such as "cpuid" should be executed on _each_ core
> before the new instruction is made visible.
> 
> Failure to do so can lead to unspecified behavior (Intel XMC erratas include
> General Protection Fault in the list), so we should avoid this at all cost.
> 
> This problem can affect modified code executed by interrupt handlers after
> interrupt are re-enabled at the end of stop_machine, because no core serializing
> instruction is executed between the code modification and the moment interrupts
> are reenabled.
> 
> Because stop_machine_text_poke performs the text modification from the first CPU
> decrementing stop_machine_first, modified code executed in thread context is
> also affected by this problem. To explain why, we have to split the CPUs in two
> categories: the CPU that initiates the text modification (calls text_poke_smp)
> and all the others. The scheduler, executed on all other CPUs after
> stop_machine, issues an "iret" core serializing instruction, and therefore
> handles core serialization for all these CPUs. However, the text modification
> initiator can continue its execution on the same thread and access the modified
> text without any scheduler call. Given that the CPU that initiates the code
> modification is not guaranteed to be the one actually performing the code
> modification, it falls into the XMC errata.

Thanks Mathieu!
It seems reasonable change. At least I'm OK :)

Reviewed-by: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>

> 
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> CC: Peter Zijlstra <peterz@infradead.org>
> CC: Arjan van de Ven <arjan@infradead.org>
> CC: "H. Peter Anvin" <hpa@zytor.com>
> CC: Thomas Gleixner <tglx@linutronix.de>
> CC: Steven Rostedt <rostedt@goodmis.org>
> CC: Ingo Molnar <mingo@elte.hu>
> CC: Andrew Morton <akpm@linux-foundation.org>
> CC: Andi Kleen <andi@firstfloor.org>
> CC: Frederic Weisbecker <fweisbec@gmail.com>
> CC: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
> ---
>  arch/x86/kernel/alternative.c |    6 ++++++
>  1 file changed, 6 insertions(+)
> 
> Index: linux-2.6-lttng/arch/x86/kernel/alternative.c
> ===================================================================
> --- linux-2.6-lttng.orig/arch/x86/kernel/alternative.c
> +++ linux-2.6-lttng/arch/x86/kernel/alternative.c
> @@ -612,6 +612,12 @@ static int __kprobes stop_machine_text_p
>  
>  	flush_icache_range((unsigned long)tpp->addr,
>  			   (unsigned long)tpp->addr + tpp->len);
> +	/*
> +	 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
> +	 * that a core serializing instruction such as "cpuid" should be
> +	 * executed on _each_ core before the new instruction is made visible.
> +	 */
> +	sync_core();
>  	return 0;
>  }
>  


-- 
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com

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

* Re: [RFC PATCH] x86: stop machine text poke should issue sync core
  2011-02-28 15:24 [RFC PATCH] x86: stop machine text poke should issue sync core Mathieu Desnoyers
  2011-03-03  5:27 ` Masami Hiramatsu
@ 2011-03-03  6:10 ` H. Peter Anvin
  2011-03-03  7:02   ` Masami Hiramatsu
  1 sibling, 1 reply; 5+ messages in thread
From: H. Peter Anvin @ 2011-03-03  6:10 UTC (permalink / raw)
  To: Mathieu Desnoyers
  Cc: Peter Zijlstra, Arjan van de Ven, Masami Hiramatsu,
	Thomas Gleixner, Steven Rostedt, Ingo Molnar, Andrew Morton,
	Andi Kleen, Frederic Weisbecker, linux-kernel

On 02/28/2011 07:24 AM, Mathieu Desnoyers wrote:
> 
> Index: linux-2.6-lttng/arch/x86/kernel/alternative.c
> ===================================================================
> --- linux-2.6-lttng.orig/arch/x86/kernel/alternative.c
> +++ linux-2.6-lttng/arch/x86/kernel/alternative.c
> @@ -612,6 +612,12 @@ static int __kprobes stop_machine_text_p
>  
>  	flush_icache_range((unsigned long)tpp->addr,
>  			   (unsigned long)tpp->addr + tpp->len);
> +	/*
> +	 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
> +	 * that a core serializing instruction such as "cpuid" should be
> +	 * executed on _each_ core before the new instruction is made visible.
> +	 */
> +	sync_core();
>  	return 0;
>  }
>  

Isn't this executed from an IPI handler, which will return with IRET (a
serializing instruction) anyway?

	-hpa

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

* Re: [RFC PATCH] x86: stop machine text poke should issue sync core
  2011-03-03  6:10 ` H. Peter Anvin
@ 2011-03-03  7:02   ` Masami Hiramatsu
  2011-03-03 15:53     ` Mathieu Desnoyers
  0 siblings, 1 reply; 5+ messages in thread
From: Masami Hiramatsu @ 2011-03-03  7:02 UTC (permalink / raw)
  To: H. Peter Anvin
  Cc: Mathieu Desnoyers, Peter Zijlstra, Arjan van de Ven,
	Thomas Gleixner, Steven Rostedt, Ingo Molnar, Andrew Morton,
	Andi Kleen, Frederic Weisbecker, linux-kernel

(2011/03/03 15:10), H. Peter Anvin wrote:
> On 02/28/2011 07:24 AM, Mathieu Desnoyers wrote:
>>
>> Index: linux-2.6-lttng/arch/x86/kernel/alternative.c
>> ===================================================================
>> --- linux-2.6-lttng.orig/arch/x86/kernel/alternative.c
>> +++ linux-2.6-lttng/arch/x86/kernel/alternative.c
>> @@ -612,6 +612,12 @@ static int __kprobes stop_machine_text_p
>>  
>>  	flush_icache_range((unsigned long)tpp->addr,
>>  			   (unsigned long)tpp->addr + tpp->len);
>> +	/*
>> +	 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
>> +	 * that a core serializing instruction such as "cpuid" should be
>> +	 * executed on _each_ core before the new instruction is made visible.
>> +	 */
>> +	sync_core();
>>  	return 0;
>>  }
>>  
> 
> Isn't this executed from an IPI handler, which will return with IRET (a
> serializing instruction) anyway?

No, now stop_machine uses per-cpu workqueue, so that handler will be
executed from worker threads. There is no iret anymore.

BTW, Mathieu, since the latest kernel has batch-text_poke_smp, this patch
needs to be updated.

Thank you,

-- 
Masami HIRAMATSU
2nd Dept. Linux Technology Center
Hitachi, Ltd., Systems Development Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com

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

* Re: [RFC PATCH] x86: stop machine text poke should issue sync core
  2011-03-03  7:02   ` Masami Hiramatsu
@ 2011-03-03 15:53     ` Mathieu Desnoyers
  0 siblings, 0 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2011-03-03 15:53 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: H. Peter Anvin, Peter Zijlstra, Arjan van de Ven, Thomas Gleixner,
	Steven Rostedt, Ingo Molnar, Andrew Morton, Andi Kleen,
	Frederic Weisbecker, linux-kernel

* Masami Hiramatsu (masami.hiramatsu.pt@hitachi.com) wrote:
> (2011/03/03 15:10), H. Peter Anvin wrote:
> > On 02/28/2011 07:24 AM, Mathieu Desnoyers wrote:
> >>
> >> Index: linux-2.6-lttng/arch/x86/kernel/alternative.c
> >> ===================================================================
> >> --- linux-2.6-lttng.orig/arch/x86/kernel/alternative.c
> >> +++ linux-2.6-lttng/arch/x86/kernel/alternative.c
> >> @@ -612,6 +612,12 @@ static int __kprobes stop_machine_text_p
> >>  
> >>  	flush_icache_range((unsigned long)tpp->addr,
> >>  			   (unsigned long)tpp->addr + tpp->len);
> >> +	/*
> >> +	 * Intel Archiecture Software Developer's Manual section 7.1.3 specifies
> >> +	 * that a core serializing instruction such as "cpuid" should be
> >> +	 * executed on _each_ core before the new instruction is made visible.
> >> +	 */
> >> +	sync_core();
> >>  	return 0;
> >>  }
> >>  
> > 
> > Isn't this executed from an IPI handler, which will return with IRET (a
> > serializing instruction) anyway?
> 
> No, now stop_machine uses per-cpu workqueue, so that handler will be
> executed from worker threads. There is no iret anymore.
> 
> BTW, Mathieu, since the latest kernel has batch-text_poke_smp, this patch
> needs to be updated.

OK, I'll update the patch and respin it.

Thanks,

Mathieu

> 
> Thank you,
> 
> -- 
> Masami HIRAMATSU
> 2nd Dept. Linux Technology Center
> Hitachi, Ltd., Systems Development Laboratory
> E-mail: masami.hiramatsu.pt@hitachi.com

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

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

end of thread, other threads:[~2011-03-03 15:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-02-28 15:24 [RFC PATCH] x86: stop machine text poke should issue sync core Mathieu Desnoyers
2011-03-03  5:27 ` Masami Hiramatsu
2011-03-03  6:10 ` H. Peter Anvin
2011-03-03  7:02   ` Masami Hiramatsu
2011-03-03 15:53     ` Mathieu Desnoyers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox