linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 2/6] x86: allow to call text_poke_bp during boot
@ 2013-10-18 14:27 Petr Mladek
  2013-10-19 15:02 ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Petr Mladek @ 2013-10-18 14:27 UTC (permalink / raw)
  To: Steven Rostedt, Frederic Weisbecker, Masami Hiramatsu, Jiri Kosina
  Cc: linux-kernel, x86, Petr Mladek

We would like to use text_poke_bp in ftrace. It might be called also during
boot when the interupts are disabled. We need to enable them for syncing
the cores on each CPU. Otherwise, there might be a deadlock, see the
warning in "smp_call_function_many", kernel/smp.c:371.

This change is taken from the current code in arch/x86/kernel/ftrace.c.

Signed-off-by: Petr Mladek <pmladek@suse.cz>
---
 arch/x86/kernel/alternative.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index f714316..13cae15 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -629,6 +629,20 @@ static void do_sync_core(void *info)
 	sync_core();
 }
 
+static void run_sync(void)
+{
+	int enable_irqs = irqs_disabled();
+
+	/* We may be called with interrupts disbled (on bootup). */
+	if (enable_irqs)
+		local_irq_enable();
+
+	on_each_cpu(do_sync_core, NULL, 1);
+
+	if (enable_irqs)
+		local_irq_disable();
+}
+
 static bool bp_patching_in_progress;
 static void *bp_int3_handler, *bp_int3_addr;
 
@@ -688,7 +702,7 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 
 	text_poke_part(addr, &int3, sizeof(int3));
 
-	on_each_cpu(do_sync_core, NULL, 1);
+	run_sync();
 
 	if (len - sizeof(int3) > 0) {
 		/* patch all but the first byte */
@@ -700,13 +714,13 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
 		 * not necessary and we'd be safe even without it. But
 		 * better safe than sorry (plus there's not only Intel).
 		 */
-		on_each_cpu(do_sync_core, NULL, 1);
+		run_sync();
 	}
 
 	/* patch the first byte */
 	text_poke_part(addr, opcode, sizeof(int3));
 
-	on_each_cpu(do_sync_core, NULL, 1);
+	run_sync();
 
 	bp_patching_in_progress = false;
 	smp_wmb();
-- 
1.8.4


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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-18 14:27 [PATCH 2/6] x86: allow to call text_poke_bp during boot Petr Mladek
@ 2013-10-19 15:02 ` Masami Hiramatsu
  2013-10-19 19:16   ` Steven Rostedt
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2013-10-19 15:02 UTC (permalink / raw)
  To: Petr Mladek
  Cc: Steven Rostedt, Frederic Weisbecker, Jiri Kosina, linux-kernel, x86

(2013/10/18 23:27), Petr Mladek wrote:
> We would like to use text_poke_bp in ftrace. It might be called also during
> boot when the interupts are disabled. We need to enable them for syncing
> the cores on each CPU. Otherwise, there might be a deadlock, see the
> warning in "smp_call_function_many", kernel/smp.c:371.

Steven, is this really needed?
I think if this is the special use(e.g. boottime test),
we'd better to run it after boot...

Thank you,

> 
> This change is taken from the current code in arch/x86/kernel/ftrace.c.
> 
> Signed-off-by: Petr Mladek <pmladek@suse.cz>
> ---
>  arch/x86/kernel/alternative.c | 20 +++++++++++++++++---
>  1 file changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
> index f714316..13cae15 100644
> --- a/arch/x86/kernel/alternative.c
> +++ b/arch/x86/kernel/alternative.c
> @@ -629,6 +629,20 @@ static void do_sync_core(void *info)
>  	sync_core();
>  }
>  
> +static void run_sync(void)
> +{
> +	int enable_irqs = irqs_disabled();
> +
> +	/* We may be called with interrupts disbled (on bootup). */
> +	if (enable_irqs)
> +		local_irq_enable();
> +
> +	on_each_cpu(do_sync_core, NULL, 1);
> +
> +	if (enable_irqs)
> +		local_irq_disable();
> +}
> +
>  static bool bp_patching_in_progress;
>  static void *bp_int3_handler, *bp_int3_addr;
>  
> @@ -688,7 +702,7 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
>  
>  	text_poke_part(addr, &int3, sizeof(int3));
>  
> -	on_each_cpu(do_sync_core, NULL, 1);
> +	run_sync();
>  
>  	if (len - sizeof(int3) > 0) {
>  		/* patch all but the first byte */
> @@ -700,13 +714,13 @@ void *text_poke_bp(void *addr, const void *opcode, size_t len, void *handler)
>  		 * not necessary and we'd be safe even without it. But
>  		 * better safe than sorry (plus there's not only Intel).
>  		 */
> -		on_each_cpu(do_sync_core, NULL, 1);
> +		run_sync();
>  	}
>  
>  	/* patch the first byte */
>  	text_poke_part(addr, opcode, sizeof(int3));
>  
> -	on_each_cpu(do_sync_core, NULL, 1);
> +	run_sync();
>  
>  	bp_patching_in_progress = false;
>  	smp_wmb();
> 


-- 
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com



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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-19 15:02 ` Masami Hiramatsu
@ 2013-10-19 19:16   ` Steven Rostedt
  2013-10-19 19:19     ` Steven Rostedt
  0 siblings, 1 reply; 9+ messages in thread
From: Steven Rostedt @ 2013-10-19 19:16 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Petr Mladek, Frederic Weisbecker, Jiri Kosina, linux-kernel, x86

On Sun, 20 Oct 2013 00:02:32 +0900
Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> wrote:

> (2013/10/18 23:27), Petr Mladek wrote:
> > We would like to use text_poke_bp in ftrace. It might be called also during
> > boot when the interupts are disabled. We need to enable them for syncing
> > the cores on each CPU. Otherwise, there might be a deadlock, see the
> > warning in "smp_call_function_many", kernel/smp.c:371.
> 
> Steven, is this really needed?
> I think if this is the special use(e.g. boottime test),
> we'd better to run it after boot...
> 

It's used to convert the calls to mcount to nops. But maybe a better
thing to do is to check if we only have a single CPU:

static void run_sync(void)
{
	if (num_online_cpus() != 1)
		on_each_cpu(do_sync_core, NULL, 1);
}


I believe that the only time we call this function with interrupts
disabled is before SMP is set up. Thus, the above change would handle
that case.

-- Steve

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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-19 19:16   ` Steven Rostedt
@ 2013-10-19 19:19     ` Steven Rostedt
  2013-10-19 21:33       ` Paul E. McKenney
  0 siblings, 1 reply; 9+ messages in thread
From: Steven Rostedt @ 2013-10-19 19:19 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Masami Hiramatsu, Petr Mladek, Frederic Weisbecker, Jiri Kosina,
	linux-kernel, x86, Paul E. McKenney


[ Added Paul because he'll understand this ]

On Sat, 19 Oct 2013 15:16:58 -0400
Steven Rostedt <rostedt@goodmis.org> wrote:

> On Sun, 20 Oct 2013 00:02:32 +0900
> Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> wrote:
> 
> > (2013/10/18 23:27), Petr Mladek wrote:
> > > We would like to use text_poke_bp in ftrace. It might be called also during
> > > boot when the interupts are disabled. We need to enable them for syncing
> > > the cores on each CPU. Otherwise, there might be a deadlock, see the
> > > warning in "smp_call_function_many", kernel/smp.c:371.
> > 
> > Steven, is this really needed?
> > I think if this is the special use(e.g. boottime test),
> > we'd better to run it after boot...
> > 
> 
> It's used to convert the calls to mcount to nops. But maybe a better
> thing to do is to check if we only have a single CPU:
> 
> static void run_sync(void)
> {
> 	if (num_online_cpus() != 1)

Hmm, to be more robust to handle our future "ideal" machines, perhaps
this should be:

	/* Ideally we would like to run on zero CPUS! */
	if (num_online_cpus() < 2)


-- Steve


> 		on_each_cpu(do_sync_core, NULL, 1);
> }
> 
> 
> I believe that the only time we call this function with interrupts
> disabled is before SMP is set up. Thus, the above change would handle
> that case.
> 
> -- Steve


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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-19 19:19     ` Steven Rostedt
@ 2013-10-19 21:33       ` Paul E. McKenney
  2013-10-19 21:58         ` Steven Rostedt
  2013-10-19 22:02         ` Steven Rostedt
  0 siblings, 2 replies; 9+ messages in thread
From: Paul E. McKenney @ 2013-10-19 21:33 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Masami Hiramatsu, Petr Mladek, Frederic Weisbecker, Jiri Kosina,
	linux-kernel, x86

On Sat, Oct 19, 2013 at 03:19:19PM -0400, Steven Rostedt wrote:
> 
> [ Added Paul because he'll understand this ]
> 
> On Sat, 19 Oct 2013 15:16:58 -0400
> Steven Rostedt <rostedt@goodmis.org> wrote:
> 
> > On Sun, 20 Oct 2013 00:02:32 +0900
> > Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> wrote:
> > 
> > > (2013/10/18 23:27), Petr Mladek wrote:
> > > > We would like to use text_poke_bp in ftrace. It might be called also during
> > > > boot when the interupts are disabled. We need to enable them for syncing
> > > > the cores on each CPU. Otherwise, there might be a deadlock, see the
> > > > warning in "smp_call_function_many", kernel/smp.c:371.
> > > 
> > > Steven, is this really needed?
> > > I think if this is the special use(e.g. boottime test),
> > > we'd better to run it after boot...
> > > 
> > 
> > It's used to convert the calls to mcount to nops. But maybe a better
> > thing to do is to check if we only have a single CPU:
> > 
> > static void run_sync(void)
> > {
> > 	if (num_online_cpus() != 1)
> 
> Hmm, to be more robust to handle our future "ideal" machines, perhaps
> this should be:
> 
> 	/* Ideally we would like to run on zero CPUS! */
> 	if (num_online_cpus() < 2)

To be really safe, shouldn't you use complex numbers?  Just in case
you end up running on a system with 5i-3 CPUs or something.  ;-)

							Thanx, Paul

> -- Steve
> 
> 
> > 		on_each_cpu(do_sync_core, NULL, 1);
> > }
> > 
> > 
> > I believe that the only time we call this function with interrupts
> > disabled is before SMP is set up. Thus, the above change would handle
> > that case.
> > 
> > -- Steve
> 


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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-19 21:33       ` Paul E. McKenney
@ 2013-10-19 21:58         ` Steven Rostedt
  2013-10-19 22:02         ` Steven Rostedt
  1 sibling, 0 replies; 9+ messages in thread
From: Steven Rostedt @ 2013-10-19 21:58 UTC (permalink / raw)
  To: paulmck
  Cc: Masami Hiramatsu, Petr Mladek, Frederic Weisbecker, Jiri Kosina,
	linux-kernel, x86

On Sat, 19 Oct 2013 14:33:50 -0700
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com> wrote:


> > 	/* Ideally we would like to run on zero CPUS! */
> > 	if (num_online_cpus() < 2)
> 
> To be really safe, shouldn't you use complex numbers?  Just in case
> you end up running on a system with 5i-3 CPUs or something.  ;-)
> 
> 							Thanx, Paul

I did think about that, but I didn't know the algorithm to test such a
case, which is why I added you to the Cc. Hoping you would come up with
a better conditional.

-- Steve


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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-19 21:33       ` Paul E. McKenney
  2013-10-19 21:58         ` Steven Rostedt
@ 2013-10-19 22:02         ` Steven Rostedt
  2013-10-20 15:42           ` Paul E. McKenney
  1 sibling, 1 reply; 9+ messages in thread
From: Steven Rostedt @ 2013-10-19 22:02 UTC (permalink / raw)
  To: paulmck
  Cc: Masami Hiramatsu, Petr Mladek, Frederic Weisbecker, Jiri Kosina,
	linux-kernel, x86

On Sat, 19 Oct 2013 14:33:50 -0700
"Paul E. McKenney" <paulmck@linux.vnet.ibm.com> wrote:

 
> > > It's used to convert the calls to mcount to nops. But maybe a better
> > > thing to do is to check if we only have a single CPU:
> > > 
> > > static void run_sync(void)
> > > {
> > > 	if (num_online_cpus() != 1)
> > 
> > Hmm, to be more robust to handle our future "ideal" machines, perhaps
> > this should be:
> > 
> > 	/* Ideally we would like to run on zero CPUS! */
> > 	if (num_online_cpus() < 2)
> 

Bah! And for such a simple computation, I got it wrong.


	/* Ideally we would like to run on zero CPUS! */
	if (num_online_cpus > 1)

But I guess the question comes. If we are running on zero CPUS, should
we perform the "on_each_cpu(do_sync_core, NULL, 1);" or not? Same goes
with 5i-3 CPUS, or negative number CPUs. If we need to do on_each_cpu(),
then I guess the != 1 will suffice.

-- Steve

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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-19 22:02         ` Steven Rostedt
@ 2013-10-20 15:42           ` Paul E. McKenney
  2013-10-28  9:18             ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Paul E. McKenney @ 2013-10-20 15:42 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: Masami Hiramatsu, Petr Mladek, Frederic Weisbecker, Jiri Kosina,
	linux-kernel, x86

On Sat, Oct 19, 2013 at 06:02:39PM -0400, Steven Rostedt wrote:
> On Sat, 19 Oct 2013 14:33:50 -0700
> "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> wrote:
> 
> 
> > > > It's used to convert the calls to mcount to nops. But maybe a better
> > > > thing to do is to check if we only have a single CPU:
> > > > 
> > > > static void run_sync(void)
> > > > {
> > > > 	if (num_online_cpus() != 1)
> > > 
> > > Hmm, to be more robust to handle our future "ideal" machines, perhaps
> > > this should be:
> > > 
> > > 	/* Ideally we would like to run on zero CPUS! */
> > > 	if (num_online_cpus() < 2)
> > 
> 
> Bah! And for such a simple computation, I got it wrong.
> 
> 
> 	/* Ideally we would like to run on zero CPUS! */
> 	if (num_online_cpus > 1)
> 
> But I guess the question comes. If we are running on zero CPUS, should
> we perform the "on_each_cpu(do_sync_core, NULL, 1);" or not? Same goes
> with 5i-3 CPUS, or negative number CPUs. If we need to do on_each_cpu(),
> then I guess the != 1 will suffice.

Makes sense to me!  Whoever adds the ability to run on zero, negative,
or complex numbers of CPUs can adjust on_each_cpu() accordingly.

							Thanx, Paul


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

* Re: [PATCH 2/6] x86: allow to call text_poke_bp during boot
  2013-10-20 15:42           ` Paul E. McKenney
@ 2013-10-28  9:18             ` Masami Hiramatsu
  0 siblings, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2013-10-28  9:18 UTC (permalink / raw)
  To: paulmck
  Cc: Steven Rostedt, Petr Mladek, Frederic Weisbecker, Jiri Kosina,
	linux-kernel, x86, yrl.pp-manager.tt

(2013/10/21 0:42), Paul E. McKenney wrote:
> On Sat, Oct 19, 2013 at 06:02:39PM -0400, Steven Rostedt wrote:
>> On Sat, 19 Oct 2013 14:33:50 -0700
>> "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> wrote:
>>
>>
>>>>> It's used to convert the calls to mcount to nops. But maybe a better
>>>>> thing to do is to check if we only have a single CPU:
>>>>>
>>>>> static void run_sync(void)
>>>>> {
>>>>> 	if (num_online_cpus() != 1)
>>>>
>>>> Hmm, to be more robust to handle our future "ideal" machines, perhaps
>>>> this should be:
>>>>
>>>> 	/* Ideally we would like to run on zero CPUS! */
>>>> 	if (num_online_cpus() < 2)
>>>
>>
>> Bah! And for such a simple computation, I got it wrong.
>>
>>
>> 	/* Ideally we would like to run on zero CPUS! */
>> 	if (num_online_cpus > 1)
>>
>> But I guess the question comes. If we are running on zero CPUS, should
>> we perform the "on_each_cpu(do_sync_core, NULL, 1);" or not? Same goes
>> with 5i-3 CPUS, or negative number CPUs. If we need to do on_each_cpu(),
>> then I guess the != 1 will suffice.
> 
> Makes sense to me!  Whoever adds the ability to run on zero, negative,
> or complex numbers of CPUs can adjust on_each_cpu() accordingly.

Thanks for making it clear!

Petr, could you update your patch according to this discussion?
I think temporally enabling irq is not a good idea.

BTW, adding an assertion(BUG_ON(irq_disabled()) at the top of text_poke_bp)
will be good for debugging.

Thanks again!

-- 
Masami HIRAMATSU
IT Management Research Dept. Linux Technology Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: masami.hiramatsu.pt@hitachi.com



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

end of thread, other threads:[~2013-10-28  9:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-10-18 14:27 [PATCH 2/6] x86: allow to call text_poke_bp during boot Petr Mladek
2013-10-19 15:02 ` Masami Hiramatsu
2013-10-19 19:16   ` Steven Rostedt
2013-10-19 19:19     ` Steven Rostedt
2013-10-19 21:33       ` Paul E. McKenney
2013-10-19 21:58         ` Steven Rostedt
2013-10-19 22:02         ` Steven Rostedt
2013-10-20 15:42           ` Paul E. McKenney
2013-10-28  9:18             ` Masami Hiramatsu

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