linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] x86/mce: Make timer handling more robust
@ 2017-01-31  8:37 Thomas Gleixner
  2017-01-31 13:33 ` Thomas Gleixner
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Thomas Gleixner @ 2017-01-31  8:37 UTC (permalink / raw)
  To: LKML; +Cc: x86, Borislav Petkov, Erik Veijola, Tony Luck

Erik reported that on a preproduction hardware a CMCI storm triggers the
BUG_ON in add_timer_on(). The reason is that the per CPU MCE timer is
started by the CMCI logic before the MCE cpu hotplug callback starts the
timer with add_timer_on(). So the timer is already queued which triggers
the BUG.

Using add_timer_on() is pretty pointless in this code because the timer is
strictlty per CPU, initialized as pinned and all operations which arm the
timer happen on the CPU to which the timer belongs.

Simplify the whole machinery by using mod_timer() instead of add_timer_on()
which avoids the problem because mod_timer() can handle already queued
timers. Use __start_timer() everywhere so the earliest armed expiry time is
preserved.

Reported-by: Erik Veijola <erik.veijola@intel.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Tony Luck <tony.luck@intel.com>
---
 arch/x86/kernel/cpu/mcheck/mce.c |   31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)

--- a/arch/x86/kernel/cpu/mcheck/mce.c
+++ b/arch/x86/kernel/cpu/mcheck/mce.c
@@ -1373,20 +1373,15 @@ static unsigned long mce_adjust_timer_de
 
 static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
 
-static void __restart_timer(struct timer_list *t, unsigned long interval)
+static void __start_timer(struct timer_list *t, unsigned long interval)
 {
 	unsigned long when = jiffies + interval;
 	unsigned long flags;
 
 	local_irq_save(flags);
 
-	if (timer_pending(t)) {
-		if (time_before(when, t->expires))
-			mod_timer(t, when);
-	} else {
-		t->expires = round_jiffies(when);
-		add_timer_on(t, smp_processor_id());
-	}
+	if (!timer_pending(t) || time_before(when, t->expires))
+		mod_timer(t, round_jiffies(when));
 
 	local_irq_restore(flags);
 }
@@ -1421,7 +1416,7 @@ static void mce_timer_fn(unsigned long d
 
 done:
 	__this_cpu_write(mce_next_interval, iv);
-	__restart_timer(t, iv);
+	__start_timer(t, iv);
 }
 
 /*
@@ -1432,7 +1427,7 @@ void mce_timer_kick(unsigned long interv
 	struct timer_list *t = this_cpu_ptr(&mce_timer);
 	unsigned long iv = __this_cpu_read(mce_next_interval);
 
-	__restart_timer(t, interval);
+	__start_timer(t, interval);
 
 	if (interval < iv)
 		__this_cpu_write(mce_next_interval, interval);
@@ -1779,17 +1774,15 @@ static void __mcheck_cpu_clear_vendor(st
 	}
 }
 
-static void mce_start_timer(unsigned int cpu, struct timer_list *t)
+static void mce_start_timer(struct timer_list *t)
 {
 	unsigned long iv = check_interval * HZ;
 
 	if (mca_cfg.ignore_ce || !iv)
 		return;
 
-	per_cpu(mce_next_interval, cpu) = iv;
-
-	t->expires = round_jiffies(jiffies + iv);
-	add_timer_on(t, cpu);
+	this_cpu_write(mce_next_interval, iv);
+	__start_timer(t, jiffies + iv);
 }
 
 static void __mcheck_cpu_setup_timer(void)
@@ -1806,7 +1799,7 @@ static void __mcheck_cpu_init_timer(void
 	unsigned int cpu = smp_processor_id();
 
 	setup_pinned_timer(t, mce_timer_fn, cpu);
-	mce_start_timer(cpu, t);
+	mce_start_timer(t);
 }
 
 /* Handle unconfigured int18 (should never happen) */
@@ -2566,7 +2559,7 @@ static int mce_cpu_dead(unsigned int cpu
 
 static int mce_cpu_online(unsigned int cpu)
 {
-	struct timer_list *t = &per_cpu(mce_timer, cpu);
+	struct timer_list *t = this_cpu_ptr(&mce_timer);
 	int ret;
 
 	mce_device_create(cpu);
@@ -2577,13 +2570,13 @@ static int mce_cpu_online(unsigned int c
 		return ret;
 	}
 	mce_reenable_cpu();
-	mce_start_timer(cpu, t);
+	mce_start_timer(t);
 	return 0;
 }
 
 static int mce_cpu_pre_down(unsigned int cpu)
 {
-	struct timer_list *t = &per_cpu(mce_timer, cpu);
+	struct timer_list *t = this_cpu_ptr(&mce_timer);
 
 	mce_disable_cpu();
 	del_timer_sync(t);

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

* Re: [PATCH] x86/mce: Make timer handling more robust
  2017-01-31  8:37 [PATCH] x86/mce: Make timer handling more robust Thomas Gleixner
@ 2017-01-31 13:33 ` Thomas Gleixner
  2017-01-31 18:37   ` Borislav Petkov
  2017-01-31 18:36 ` Borislav Petkov
  2017-01-31 20:51 ` [tip:x86/urgent] " tip-bot for Thomas Gleixner
  2 siblings, 1 reply; 6+ messages in thread
From: Thomas Gleixner @ 2017-01-31 13:33 UTC (permalink / raw)
  To: LKML; +Cc: x86, Borislav Petkov, Erik Veijola, Tony Luck

On Tue, 31 Jan 2017, Thomas Gleixner wrote:
> +static void mce_start_timer(struct timer_list *t)
>  {
>  	unsigned long iv = check_interval * HZ;
>  
>  	if (mca_cfg.ignore_ce || !iv)
>  		return;
>  
> -	per_cpu(mce_next_interval, cpu) = iv;
> -
> -	t->expires = round_jiffies(jiffies + iv);
> -	add_timer_on(t, cpu);
> +	this_cpu_write(mce_next_interval, iv);
> +	__start_timer(t, jiffies + iv);

Bah. That's wrong. Delta patch below:

--- a/arch/x86/kernel/cpu/mcheck/mce.c
+++ b/arch/x86/kernel/cpu/mcheck/mce.c
@@ -1782,7 +1782,7 @@ static void mce_start_timer(struct timer
 		return;
 
 	this_cpu_write(mce_next_interval, iv);
-	__start_timer(t, jiffies + iv);
+	__start_timer(t, iv);
 }
 
 static void __mcheck_cpu_setup_timer(void)

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

* Re: [PATCH] x86/mce: Make timer handling more robust
  2017-01-31  8:37 [PATCH] x86/mce: Make timer handling more robust Thomas Gleixner
  2017-01-31 13:33 ` Thomas Gleixner
@ 2017-01-31 18:36 ` Borislav Petkov
  2017-01-31 20:51 ` [tip:x86/urgent] " tip-bot for Thomas Gleixner
  2 siblings, 0 replies; 6+ messages in thread
From: Borislav Petkov @ 2017-01-31 18:36 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, x86, Erik Veijola, Tony Luck

On Tue, Jan 31, 2017 at 09:37:34AM +0100, Thomas Gleixner wrote:
> Erik reported that on a preproduction hardware a CMCI storm triggers the
> BUG_ON in add_timer_on(). The reason is that the per CPU MCE timer is
> started by the CMCI logic before the MCE cpu hotplug callback starts the

nitpick:				   CPU

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

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

* Re: [PATCH] x86/mce: Make timer handling more robust
  2017-01-31 13:33 ` Thomas Gleixner
@ 2017-01-31 18:37   ` Borislav Petkov
  2017-02-01  9:29     ` Veijola, Erik
  0 siblings, 1 reply; 6+ messages in thread
From: Borislav Petkov @ 2017-01-31 18:37 UTC (permalink / raw)
  To: Thomas Gleixner; +Cc: LKML, x86, Erik Veijola, Tony Luck

On Tue, Jan 31, 2017 at 02:33:29PM +0100, Thomas Gleixner wrote:
> On Tue, 31 Jan 2017, Thomas Gleixner wrote:
> > +static void mce_start_timer(struct timer_list *t)
> >  {
> >  	unsigned long iv = check_interval * HZ;
> >  
> >  	if (mca_cfg.ignore_ce || !iv)
> >  		return;
> >  
> > -	per_cpu(mce_next_interval, cpu) = iv;
> > -
> > -	t->expires = round_jiffies(jiffies + iv);
> > -	add_timer_on(t, cpu);
> > +	this_cpu_write(mce_next_interval, iv);
> > +	__start_timer(t, jiffies + iv);
> 
> Bah. That's wrong. Delta patch below:
> 
> --- a/arch/x86/kernel/cpu/mcheck/mce.c
> +++ b/arch/x86/kernel/cpu/mcheck/mce.c
> @@ -1782,7 +1782,7 @@ static void mce_start_timer(struct timer
>  		return;
>  
>  	this_cpu_write(mce_next_interval, iv);
> -	__start_timer(t, jiffies + iv);
> +	__start_timer(t, iv);
>  }
>  
>  static void __mcheck_cpu_setup_timer(void)

With that hunk merged in:

Reviewed-by: Borislav Petkov <bp@suse.de>
Tested-by: Borislav Petkov <bp@suse.de>

Thanks!

-- 
Regards/Gruss,
    Boris.

Good mailing practices for 400: avoid top-posting and trim the reply.

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

* [tip:x86/urgent] x86/mce: Make timer handling more robust
  2017-01-31  8:37 [PATCH] x86/mce: Make timer handling more robust Thomas Gleixner
  2017-01-31 13:33 ` Thomas Gleixner
  2017-01-31 18:36 ` Borislav Petkov
@ 2017-01-31 20:51 ` tip-bot for Thomas Gleixner
  2 siblings, 0 replies; 6+ messages in thread
From: tip-bot for Thomas Gleixner @ 2017-01-31 20:51 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: hpa, mingo, erik.veijola, tglx, bp, tony.luck, linux-kernel

Commit-ID:  0becc0ae5b42828785b589f686725ff5bc3b9b25
Gitweb:     http://git.kernel.org/tip/0becc0ae5b42828785b589f686725ff5bc3b9b25
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 31 Jan 2017 09:37:34 +0100
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Tue, 31 Jan 2017 21:47:58 +0100

x86/mce: Make timer handling more robust

Erik reported that on a preproduction hardware a CMCI storm triggers the
BUG_ON in add_timer_on(). The reason is that the per CPU MCE timer is
started by the CMCI logic before the MCE CPU hotplug callback starts the
timer with add_timer_on(). So the timer is already queued which triggers
the BUG.

Using add_timer_on() is pretty pointless in this code because the timer is
strictlty per CPU, initialized as pinned and all operations which arm the
timer happen on the CPU to which the timer belongs.

Simplify the whole machinery by using mod_timer() instead of add_timer_on()
which avoids the problem because mod_timer() can handle already queued
timers. Use __start_timer() everywhere so the earliest armed expiry time is
preserved.

Reported-by: Erik Veijola <erik.veijola@intel.com>
Tested-by: Borislav Petkov <bp@alien8.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Borislav Petkov <bp@alien8.de>
Cc: Tony Luck <tony.luck@intel.com>
Link: http://lkml.kernel.org/r/alpine.DEB.2.20.1701310936080.3457@nanos
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>

---
 arch/x86/kernel/cpu/mcheck/mce.c | 31 ++++++++++++-------------------
 1 file changed, 12 insertions(+), 19 deletions(-)

diff --git a/arch/x86/kernel/cpu/mcheck/mce.c b/arch/x86/kernel/cpu/mcheck/mce.c
index 00ef432..537c664 100644
--- a/arch/x86/kernel/cpu/mcheck/mce.c
+++ b/arch/x86/kernel/cpu/mcheck/mce.c
@@ -1373,20 +1373,15 @@ static unsigned long mce_adjust_timer_default(unsigned long interval)
 
 static unsigned long (*mce_adjust_timer)(unsigned long interval) = mce_adjust_timer_default;
 
-static void __restart_timer(struct timer_list *t, unsigned long interval)
+static void __start_timer(struct timer_list *t, unsigned long interval)
 {
 	unsigned long when = jiffies + interval;
 	unsigned long flags;
 
 	local_irq_save(flags);
 
-	if (timer_pending(t)) {
-		if (time_before(when, t->expires))
-			mod_timer(t, when);
-	} else {
-		t->expires = round_jiffies(when);
-		add_timer_on(t, smp_processor_id());
-	}
+	if (!timer_pending(t) || time_before(when, t->expires))
+		mod_timer(t, round_jiffies(when));
 
 	local_irq_restore(flags);
 }
@@ -1421,7 +1416,7 @@ static void mce_timer_fn(unsigned long data)
 
 done:
 	__this_cpu_write(mce_next_interval, iv);
-	__restart_timer(t, iv);
+	__start_timer(t, iv);
 }
 
 /*
@@ -1432,7 +1427,7 @@ void mce_timer_kick(unsigned long interval)
 	struct timer_list *t = this_cpu_ptr(&mce_timer);
 	unsigned long iv = __this_cpu_read(mce_next_interval);
 
-	__restart_timer(t, interval);
+	__start_timer(t, interval);
 
 	if (interval < iv)
 		__this_cpu_write(mce_next_interval, interval);
@@ -1779,17 +1774,15 @@ static void __mcheck_cpu_clear_vendor(struct cpuinfo_x86 *c)
 	}
 }
 
-static void mce_start_timer(unsigned int cpu, struct timer_list *t)
+static void mce_start_timer(struct timer_list *t)
 {
 	unsigned long iv = check_interval * HZ;
 
 	if (mca_cfg.ignore_ce || !iv)
 		return;
 
-	per_cpu(mce_next_interval, cpu) = iv;
-
-	t->expires = round_jiffies(jiffies + iv);
-	add_timer_on(t, cpu);
+	this_cpu_write(mce_next_interval, iv);
+	__start_timer(t, iv);
 }
 
 static void __mcheck_cpu_setup_timer(void)
@@ -1806,7 +1799,7 @@ static void __mcheck_cpu_init_timer(void)
 	unsigned int cpu = smp_processor_id();
 
 	setup_pinned_timer(t, mce_timer_fn, cpu);
-	mce_start_timer(cpu, t);
+	mce_start_timer(t);
 }
 
 /* Handle unconfigured int18 (should never happen) */
@@ -2566,7 +2559,7 @@ static int mce_cpu_dead(unsigned int cpu)
 
 static int mce_cpu_online(unsigned int cpu)
 {
-	struct timer_list *t = &per_cpu(mce_timer, cpu);
+	struct timer_list *t = this_cpu_ptr(&mce_timer);
 	int ret;
 
 	mce_device_create(cpu);
@@ -2577,13 +2570,13 @@ static int mce_cpu_online(unsigned int cpu)
 		return ret;
 	}
 	mce_reenable_cpu();
-	mce_start_timer(cpu, t);
+	mce_start_timer(t);
 	return 0;
 }
 
 static int mce_cpu_pre_down(unsigned int cpu)
 {
-	struct timer_list *t = &per_cpu(mce_timer, cpu);
+	struct timer_list *t = this_cpu_ptr(&mce_timer);
 
 	mce_disable_cpu();
 	del_timer_sync(t);

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

* Re: [PATCH] x86/mce: Make timer handling more robust
  2017-01-31 18:37   ` Borislav Petkov
@ 2017-02-01  9:29     ` Veijola, Erik
  0 siblings, 0 replies; 6+ messages in thread
From: Veijola, Erik @ 2017-02-01  9:29 UTC (permalink / raw)
  To: tglx, bp; +Cc: linux-kernel, Luck, Tony, x86

On Tue, 2017-01-31 at 19:37 +0100, Borislav Petkov wrote:
> Bah. That's wrong. Delta patch below:
> > 
> > --- a/arch/x86/kernel/cpu/mcheck/mce.c
> > +++ b/arch/x86/kernel/cpu/mcheck/mce.c
> > @@ -1782,7 +1782,7 @@ static void mce_start_timer(struct timer
> >  		return;
> >  
> >  	this_cpu_write(mce_next_interval, iv);
> > -	__start_timer(t, jiffies + iv);
> > +	__start_timer(t, iv);
> >  }
> >  
> >  static void __mcheck_cpu_setup_timer(void)
> 
> With that hunk merged in:
> 
> Reviewed-by: Borislav Petkov <bp@suse.de>
> Tested-by: Borislav Petkov <bp@suse.de>
> 
> Thanks!
> 

Tested this on the preproduction hardware:

Tested-by: Erik Veijola <erik.veijola@intel.com>

-Erik
---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki 
Business Identity Code: 0357606 - 4 
Domiciled in Helsinki 

This e-mail and any attachments may contain confidential material for
the sole use of the intended recipient(s). Any review or distribution
by others is strictly prohibited. If you are not the intended
recipient, please contact the sender and delete all copies.

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

end of thread, other threads:[~2017-02-01  9:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-01-31  8:37 [PATCH] x86/mce: Make timer handling more robust Thomas Gleixner
2017-01-31 13:33 ` Thomas Gleixner
2017-01-31 18:37   ` Borislav Petkov
2017-02-01  9:29     ` Veijola, Erik
2017-01-31 18:36 ` Borislav Petkov
2017-01-31 20:51 ` [tip:x86/urgent] " tip-bot for Thomas Gleixner

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