Skip to contentSkip to conversation

x86/nmi: remove the irqwork from long duration nmi handler

PATCH Superseded Changbin Du 3 participants 7 messages Jan 1, 2020 – Jan 11, 2020 2 messages not in the archive x86

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/nmi: remove the irqwork from long duration nmi handler

First, printk is NMI context safe now since the safe printk has been implemented. The safe printk already has an irqwork to make NMI context safe.

Second, the NMI irqwork actually does not work if a NMI handler causes panic by watchdog timeout. This NMI irqwork have no chance to run in such case, while the safe printk will flush its per-cpu buffer before panic.

---
 arch/x86/include/asm/nmi.h |  1 -
 arch/x86/kernel/nmi.c      | 20 +++++++++-----------
 2 files changed, 9 insertions(+), 12 deletions(-)
arch/x86/include/asm/nmi.h +0 -1
index 75ded1d13d98..9d5d949e662e 100644
@@ -41,7 +41,6 @@ struct nmiaction {
 	struct list_head	list;
 	nmi_handler_t		handler;
 	u64			max_duration;
-	struct irq_work		irq_work;
 	unsigned long		flags;
 	const char		*name;
 };
arch/x86/kernel/nmi.c +9 -11
index e676a9916c49..aa15d4f2340f 100644
@@ -104,18 +104,22 @@ static int __init nmi_warning_debugfs(void)
 }
 fs_initcall(nmi_warning_debugfs);
 
-static void nmi_max_handler(struct irq_work *w)
+static void nmi_check_duration(struct nmiaction *action, u64 duration)
 {
-	struct nmiaction *a = container_of(w, struct nmiaction, irq_work);
 	int remainder_ns, decimal_msecs;
-	u64 whole_msecs = READ_ONCE(a->max_duration);
+	u64 whole_msecs = READ_ONCE(action->max_duration);
+
+	if (duration < nmi_longest_ns || duration < action->max_duration)
+		return;
+
+	action->max_duration = duration;
 
 	remainder_ns = do_div(whole_msecs, (1000 * 1000));
 	decimal_msecs = remainder_ns / 1000;
 
 	printk_ratelimited(KERN_INFO
 		"INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
-		a->handler, whole_msecs, decimal_msecs);
+		action->handler, whole_msecs, decimal_msecs);
 }
 
 static int nmi_handle(unsigned int type, struct pt_regs *regs)
@@ -142,11 +146,7 @@ static int nmi_handle(unsigned int type, struct pt_regs *regs)
 		delta = sched_clock() - delta;
 		trace_nmi_handler(a->handler, (int)delta, thishandled);
 
-		if (delta < nmi_longest_ns || delta < a->max_duration)
-			continue;
-
-		a->max_duration = delta;
-		irq_work_queue(&a->irq_work);
+		nmi_check_duration(a, delta);
 	}
 
 	rcu_read_unlock();
@@ -164,8 +164,6 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)
 	if (!action->handler)
 		return -EINVAL;
 
-	init_irq_work(&action->irq_work, nmi_max_handler);
-
 	raw_spin_lock_irqsave(&desc->lock, flags);
 
 	/*
Signature · 1 line
2.24.0
#1 lore raw

Hi, Thomas,
Have you checked this one? I think this even can consider as a fix.

Quoted 82 lines · Changbin Du

First, printk is NMI context safe now since the safe printk has been implemented. The safe printk already has an irqwork to make NMI context safe.

Second, the NMI irqwork actually does not work if a NMI handler causes panic by watchdog timeout. This NMI irqwork have no chance to run in such case, while the safe printk will flush its per-cpu buffer before panic.

---
 arch/x86/include/asm/nmi.h |  1 -
 arch/x86/kernel/nmi.c      | 20 +++++++++-----------
 2 files changed, 9 insertions(+), 12 deletions(-)
arch/x86/include/asm/nmi.h +0 -1
index 75ded1d13d98..9d5d949e662e 100644
@@ -41,7 +41,6 @@ struct nmiaction {
 	struct list_head	list;
 	nmi_handler_t		handler;
 	u64			max_duration;
-	struct irq_work		irq_work;
 	unsigned long		flags;
 	const char		*name;
 };
arch/x86/kernel/nmi.c +9 -11
index e676a9916c49..aa15d4f2340f 100644
@@ -104,18 +104,22 @@ static int __init nmi_warning_debugfs(void)
 }
 fs_initcall(nmi_warning_debugfs);
 
-static void nmi_max_handler(struct irq_work *w)
+static void nmi_check_duration(struct nmiaction *action, u64 duration)
 {
-	struct nmiaction *a = container_of(w, struct nmiaction, irq_work);
 	int remainder_ns, decimal_msecs;
-	u64 whole_msecs = READ_ONCE(a->max_duration);
+	u64 whole_msecs = READ_ONCE(action->max_duration);
+
+	if (duration < nmi_longest_ns || duration < action->max_duration)
+		return;
+
+	action->max_duration = duration;
 
 	remainder_ns = do_div(whole_msecs, (1000 * 1000));
 	decimal_msecs = remainder_ns / 1000;
 
 	printk_ratelimited(KERN_INFO
 		"INFO: NMI handler (%ps) took too long to run: %lld.%03d msecs\n",
-		a->handler, whole_msecs, decimal_msecs);
+		action->handler, whole_msecs, decimal_msecs);
 }
 
 static int nmi_handle(unsigned int type, struct pt_regs *regs)
@@ -142,11 +146,7 @@ static int nmi_handle(unsigned int type, struct pt_regs *regs)
 		delta = sched_clock() - delta;
 		trace_nmi_handler(a->handler, (int)delta, thishandled);
 
-		if (delta < nmi_longest_ns || delta < a->max_duration)
-			continue;
-
-		a->max_duration = delta;
-		irq_work_queue(&a->irq_work);
+		nmi_check_duration(a, delta);
 	}
 
 	rcu_read_unlock();
@@ -164,8 +164,6 @@ int __register_nmi_handler(unsigned int type, struct nmiaction *action)
 	if (!action->handler)
 		return -EINVAL;
 
-	init_irq_work(&action->irq_work, nmi_max_handler);
-
 	raw_spin_lock_irqsave(&desc->lock, flags);
 
 	/*
Signature · 1 line
2.24.0
Signature · 2 lines
Cheers,
Changbin Du
Acked-by on v1 1/1
#2 lore raw
In reply to Changbin Du
Quoted 9 lines · Changbin Du

First, printk is NMI context safe now since the safe printk has been implemented. The safe printk already has an irqwork to make NMI context safe.

Second, the NMI irqwork actually does not work if a NMI handler causes panic by watchdog timeout. This NMI irqwork have no chance to run in such case, while the safe printk will flush its per-cpu buffer before panic.

Looks about right.

maintainer
#3 lore raw
Quoted 10 lines · Thomas Gleixner
Quoted 8 lines · Changbin Du

First, printk is NMI context safe now since the safe printk has been implemented. The safe printk already has an irqwork to make NMI context safe.

Second, the NMI irqwork actually does not work if a NMI handler causes panic by watchdog timeout. This NMI irqwork have no chance to run in such case, while the safe printk will flush its per-cpu buffer before panic.

Quoted 5 lines
Quoted 1 line

Looks about right.

I'm wondering why is this thing being moved:

- if (delta < nmi_longest_ns || delta < a->max_duration)
- continue;

into nmi_check_duration() and not remaining where it is?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#6 lore raw
In reply to a message not in the archive
Quoted 7 lines · Borislav Petkov
Quoted 2 lines · Changbin Du

I added a new function nmi_check_duration(), so shoudn't this check be done in that function?

Why should it be done in that function? Your patch is removing irq_work
- why is it doing additional changes?

Just to move all the check code together and be a standalone function. yes, this somewhat does code refining after the irqwork is removed but I think it is normal.

Quoted 8 lines
Quoted 2 lines

Don't worry about performance, this function will be inlined by compiler.

I'm not worried about that at all.

Btw, why are you sending private mail and not keeping the discussion on the mailing list?

oops, typed wrong key. Just added back.

Quoted 5 lines
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Signature · 2 lines
Cheers,
Changbin Du
maintainer
#7 lore raw
Quoted 3 lines · Changbin Du

Just to move all the check code together and be a standalone function. yes, this somewhat does code refining after the irqwork is removed but I think it is normal.

But it makes review harder because your patch is removing irq_work, *nothing* in the commit message is talking about *why* you're doing that additional change. I'd imagine at the end of the commit message something like:

"While at it, repurpose the IRQ work callback into a function which concentrates the NMI duration checking."

This lets a reader know know why that additional change is done instead of going back'n'forth and having to ask you why you're doing this.

Ok?

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#8 lore raw
Quoted 10 lines · Borislav Petkov
Quoted 3 lines · Changbin Du

Just to move all the check code together and be a standalone function. yes, this somewhat does code refining after the irqwork is removed but I think it is normal.

But it makes review harder because your patch is removing irq_work, *nothing* in the commit message is talking about *why* you're doing that additional change. I'd imagine at the end of the commit message something like:

Quoted 8 lines

"While at it, repurpose the IRQ work callback into a function which concentrates the NMI duration checking."

This lets a reader know know why that additional change is done instead of going back'n'forth and having to ask you why you're doing this.

Ok?

sure, and thanks for your suggestion. I will send v2 later.

Quoted 5 lines
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Signature · 2 lines
Cheers,
Changbin Du