Skip to contentSkip to conversation

genirq: add migratable NMI registration and show NMI counts

RFC PATCH In review Ashok Raj 3 participants 10 messages Nov 14 – Nov 19 IRQ

lore
Share & tools
lore

Ashok Raj

author
#0 lore raw

cover genirq: add migratable NMI registration and show NMI counts

request_nmi() currently models NMIs as per-CPU sources. This does not represent a globally routed NMI source with an affinity-capable irqchip, which needs to be rerouted when its target CPU is offlined.

This RFC adds IRQF_NMI_MIGRATABLE as a request_nmi() registration mode for such a source. It remains excluded from normal IRQ balancing, but the generic startup and CPU-hotplug paths may select an online target. The second patch fixes the related /proc/interrupts display: NMI-safe flow handling updates the per-CPU counters rather than irq_desc::tot_count.

Patch 1: Add the migratable-NMI registration mode, validate that it is used only through request_nmi() with an affinity-capable non-per-CPU irqchip, and add IRQ KUnit coverage for registration and CPU-offline migration.

Patch 2: Emit the per-CPU counters for all NMI descriptors, matching kstat_irqs_desc().

Testing:
- ARM64 changed-object builds with WERROR=1
- ARM64 !SMP changed-object build
- ARM64 IRQ KUnit run under QEMU with two CPUs, including registration validation and CPU-offline migration
- local downstream GHES delivery and migration validation for the intended consumer topology (not part of this series)

Base and dependencies:

This RFC was developed and tested on v7.4-rc2 plus the FEAT_NMI v2 series [1]. It depends on that series' GIC NMI support and is therefore not directly applicable to mainline or linux-next as posted. This RFC is for design feedback; it will need rebasing onto the upstream form of those prerequisites before submission.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

A separate GHES Type-4 series is the intended first consumer and is not included here.

Feedback is particularly welcome on the IRQF_NMI_MIGRATABLE API and its placement in the generic IRQ API, as well as the NMI accounting presentation in /proc/interrupts.

Ashok Raj (2):
genirq: Add migratable NMI registration mode genirq/proc: Show NMI interrupt counts in /proc/interrupts

 include/linux/interrupt.h |   2 +
 kernel/irq/irq_test.c     | 141 +++++++++++++++++++++++++++++++++++++-
 kernel/irq/manage.c       |  33 +++++++--
 kernel/irq/proc.c         |   7 +-
 4 files changed, 172 insertions(+), 11 deletions(-)

--
2.43.0

author
#1 lore raw

RFC PATCH 1/2 genirq: Add migratable NMI registration mode

request_nmi() only supports per-CPU NMIs, so a globally routed NMI source cannot use the generic CPU-offline IRQ migration path.

Add IRQF_NMI_MIGRATABLE for a non-per-CPU NMI. Require an affinity-capable irqchip, preserve the NMI no-balancing policy, and allow generic startup affinity and CPU-hotplug migration to route the source. Keep local per-CPU NMIs unchanged.

Add KUnit coverage for registration validation and CPU-offline migration.

---
 include/linux/interrupt.h |   2 +
 kernel/irq/irq_test.c     | 141 +++++++++++++++++++++++++++++++++++++-
 kernel/irq/manage.c       |  33 +++++++--
 3 files changed, 168 insertions(+), 8 deletions(-)
include/linux/interrupt.h +2 -0
index ee2dd5349697..58c917ce7655 100644
@@ -46,6 +46,7 @@
  * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
  * IRQF_PERCPU - Interrupt is per cpu
  * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
+ * IRQF_NMI_MIGRATABLE - NMI source can be migrated during CPU hotplug
  * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
  *                registered first in a shared interrupt is considered for
  *                performance reasons)
@@ -86,6 +87,7 @@
 #define IRQF_NO_AUTOEN		0x00080000
 #define IRQF_NO_DEBUG		0x00100000
 #define IRQF_COND_ONESHOT	0x00200000
+#define IRQF_NMI_MIGRATABLE	0x00400000
 
 #define IRQF_TIMER		(__IRQF_TIMER | IRQF_NO_SUSPEND | IRQF_NO_THREAD)
 
kernel/irq/irq_test.c +140 -1

171 lines not on this page · raw message

kernel/irq/manage.c +26 -7
index 9c32607f1ac3..d9f28e40affc 100644
@@ -155,6 +155,8 @@ EXPORT_SYMBOL(synchronize_irq);
 #ifdef CONFIG_SMP
 cpumask_var_t irq_default_affinity;
 
+static bool irq_is_migratable_nmi(struct irq_desc *desc);
+
 static bool __irq_can_set_affinity(struct irq_desc *desc)
 {
 	if (!desc || !irqd_can_balance(&desc->irq_data) ||
@@ -599,8 +601,8 @@ int irq_setup_affinity(struct irq_desc *desc)
 	static DEFINE_RAW_SPINLOCK(mask_lock);
 	static struct cpumask mask;
 
-	/* Excludes PER_CPU and NO_BALANCE interrupts */
-	if (!__irq_can_set_affinity(desc))
+	/* Excludes PER_CPU and NO_BALANCE interrupts. */
+	if (!__irq_can_set_affinity(desc) && !irq_is_migratable_nmi(desc))
 		return 0;
 
 	guard(raw_spinlock)(&mask_lock);
@@ -1384,6 +1386,15 @@ static bool irq_supports_nmi(struct irq_desc *desc)
 	return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
 }
 
+#ifdef CONFIG_SMP
+static bool irq_is_migratable_nmi(struct irq_desc *desc)
+{
+	return irq_is_nmi(desc) && desc->action &&
+	       (desc->action->flags & IRQF_NMI_MIGRATABLE) &&
+	       desc->irq_data.chip->irq_set_affinity;
+}
+#endif
+
 static int irq_nmi_setup(struct irq_desc *desc)
 {
 	struct irq_data *d = irq_desc_get_irq_data(desc);
@@ -2153,7 +2164,8 @@ int request_threaded_irq(unsigned int irq, irq_handler_t handler,
 	 * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
 	 * it cannot be set along with IRQF_NO_SUSPEND.
 	 */
-	if (((irqflags & IRQF_SHARED) && !dev_id) ||
+	if ((irqflags & IRQF_NMI_MIGRATABLE) ||
+	    ((irqflags & IRQF_SHARED) && !dev_id) ||
 	    ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) ||
 	    (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
 	    ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
@@ -2275,8 +2287,9 @@ EXPORT_SYMBOL_GPL(request_any_context_irq);
  * An interrupt line delivering NMIs cannot be shared and IRQ handling
  * cannot be threaded.
  *
- * Interrupt lines requested for NMI delivering must produce per cpu
- * interrupts and have auto enabling setting disabled.
+ * Interrupt lines requested for NMI delivery must either produce per CPU
+ * interrupts or be migratable during CPU hotplug, and must have auto enabling
+ * disabled.
  *
  * @dev_id must be globally unique. Normally the address of the device data
  * structure is used as the cookie. Since the handler receives this value
@@ -2299,7 +2312,9 @@ int request_nmi(unsigned int irq, irq_handler_t handler,
 	if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
 		return -EINVAL;
 
-	if (!(irqflags & IRQF_PERCPU))
+	/* Exactly one NMI topology must be specified. */
+	if (!!(irqflags & IRQF_PERCPU) ==
+	    !!(irqflags & IRQF_NMI_MIGRATABLE))
 		return -EINVAL;
 
 	if (!handler)
@@ -2311,7 +2326,11 @@ int request_nmi(unsigned int irq, irq_handler_t handler,
 	    !(irqflags & IRQF_NO_AUTOEN)) ||
 	    !irq_settings_can_request(desc) ||
 	    WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
-	    !irq_supports_nmi(desc))
+	    !irq_supports_nmi(desc) ||
+	    ((irqflags & IRQF_NMI_MIGRATABLE) &&
+	     (irqd_is_per_cpu(&desc->irq_data) ||
+	      irq_settings_is_per_cpu(desc) ||
+	      !desc->irq_data.chip->irq_set_affinity)))
 		return -EINVAL;
 
 	action = kzalloc_obj(struct irqaction);
Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH 2/2 genirq/proc: Show NMI interrupt counts in /proc/interrupts

In reply to Ashok Raj

NMI flow handlers update only the per-CPU counters because irq_desc::tot_count is not safe to update in NMI context. As a result, /proc/interrupts shows zero counts for a non-per-CPU NMI.

Display the per-CPU counters for NMI descriptors, matching kstat_irqs_desc().

---
 kernel/irq/proc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
kernel/irq/proc.c +4 -3
index 0c0df16de581..490b2df9c105 100644
@@ -559,11 +559,12 @@ static int irq_seq_show(struct seq_file *p, void *v)
 	seq_putc(p, ':');
 
 	/*
-	 * Always output per CPU interrupts. Output device interrupts only when
-	 * desc::tot_count is not zero.
+	 * Always output per CPU interrupts and NMIs, which only account in
+	 * per-CPU counters. Output device interrupts only when desc::tot_count
+	 * is not zero.
 	 */
 	if (irq_settings_is_per_cpu(desc) || irq_settings_is_per_cpu_devid(desc) ||
-	    data_race(desc->tot_count))
+	    irq_is_nmi(desc) || data_race(desc->tot_count))
 		irq_proc_emit_counts(p, &desc->kstat_irqs->cnt);
 	else
 		irq_proc_emit_zero_counts(p, num_online_cpus());
Signature · 1 line
2.43.0
#3 lore raw
In reply to Ashok Raj

Hi Ashok,

Quoted 28 lines · Ashok Raj

request_nmi() currently models NMIs as per-CPU sources. This does not represent a globally routed NMI source with an affinity-capable irqchip, which needs to be rerouted when its target CPU is offlined.

This RFC adds IRQF_NMI_MIGRATABLE as a request_nmi() registration mode for such a source. It remains excluded from normal IRQ balancing, but the generic startup and CPU-hotplug paths may select an online target. The second patch fixes the related /proc/interrupts display: NMI-safe flow handling updates the per-CPU counters rather than irq_desc::tot_count.

Patch 1: Add the migratable-NMI registration mode, validate that it is used only through request_nmi() with an affinity-capable non-per-CPU irqchip, and add IRQ KUnit coverage for registration and CPU-offline migration.

Patch 2: Emit the per-CPU counters for all NMI descriptors, matching kstat_irqs_desc().

Testing:
- ARM64 changed-object builds with WERROR=1
- ARM64 !SMP changed-object build
- ARM64 IRQ KUnit run under QEMU with two CPUs, including registration validation and CPU-offline migration
- local downstream GHES delivery and migration validation for the intended consumer topology (not part of this series)

Base and dependencies:

Quoted 8 lines

This RFC was developed and tested on v7.4-rc2 plus the FEAT_NMI v2 series [1]. It depends on that series' GIC NMI support and is therefore not directly applicable to mainline or linux-next as posted. This RFC is for design feedback; it will need rebasing onto the upstream form of those prerequisites before submission.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

I'm not an expert in this area, but based on what I've learned so far, NMI being per-CPU like makes life much easier ;)

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. Consider:

CPU0 CPU1 CPU2

---------------------------------------------------------------------------

<NMI>

handling started
set NMI affinity to CPU2
<NMI> handling started
mutate shared state
mutate shared state
handling finished
handling finished

IOW, the same NMI handler could execute concurrently on two CPUs, potentially corrupting shared state. Once again more than happy to be corrected if I missed something :)

Cheers
Vladimir

Quoted 21 lines

A separate GHES Type-4 series is the intended first consumer and is not included here.

Feedback is particularly welcome on the IRQF_NMI_MIGRATABLE API and its placement in the generic IRQ API, as well as the NMI accounting presentation in /proc/interrupts.

Ashok Raj (2):
genirq: Add migratable NMI registration mode genirq/proc: Show NMI interrupt counts in /proc/interrupts

 include/linux/interrupt.h |   2 +
 kernel/irq/irq_test.c     | 141 +++++++++++++++++++++++++++++++++++++-
 kernel/irq/manage.c       |  33 +++++++--
 kernel/irq/proc.c         |   7 +-
 4 files changed, 172 insertions(+), 11 deletions(-)

--
2.43.0

author
#4 lore raw

Hi Vladimir

Quoted 37 lines · Vladimir Murzin

Hi Ashok,

Quoted 34 lines · Ashok Raj

request_nmi() currently models NMIs as per-CPU sources. This does not represent a globally routed NMI source with an affinity-capable irqchip, which needs to be rerouted when its target CPU is offlined.

This RFC adds IRQF_NMI_MIGRATABLE as a request_nmi() registration mode for such a source. It remains excluded from normal IRQ balancing, but the generic startup and CPU-hotplug paths may select an online target. The second patch fixes the related /proc/interrupts display: NMI-safe flow handling updates the per-CPU counters rather than irq_desc::tot_count.

Patch 1: Add the migratable-NMI registration mode, validate that it is used only through request_nmi() with an affinity-capable non-per-CPU irqchip, and add IRQ KUnit coverage for registration and CPU-offline migration.

Patch 2: Emit the per-CPU counters for all NMI descriptors, matching kstat_irqs_desc().

Testing:
- ARM64 changed-object builds with WERROR=1
- ARM64 !SMP changed-object build
- ARM64 IRQ KUnit run under QEMU with two CPUs, including registration validation and CPU-offline migration
- local downstream GHES delivery and migration validation for the intended consumer topology (not part of this series)

Base and dependencies:

This RFC was developed and tested on v7.4-rc2 plus the FEAT_NMI v2 series [1]. It depends on that series' GIC NMI support and is therefore not directly applicable to mainline or linux-next as posted. This RFC is for design feedback; it will need rebasing onto the upstream form of those prerequisites before submission.

Quoted 5 lines
Quoted 2 lines

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

I'm not an expert in this area, but based on what I've learned so far, NMI being per-CPU like makes life much easier ;)

I'm still learning the ARM side :-), so please correct me if I've got something wrong.

The events I'm working on are platform RAS events described via:

	HEST -> GHES entries. 

My prototype uses an extended Type-4 GHES entry (Type-4 is
just for the RFC discussion here; I also have an alternative based on Marc's earlier suggestion).

These are SPIs, so they can be routed to any CPU. But request_nmi() as structured picks one CPU and pins the interrupt to it, treating it as per-CPU. If that CPU goes offline, the interrupt is orphaned.

I see two ways to handle this:

Option 1: Retarget on CPU offline. My POC registers a hotplug callback that picks a new target CPU. If none is available (e.g. this is the last online CPU), it vetoes the offline. This works, but it feels like a band-aid.

Option 2: Extend the API for SPIs. Add a flag that lets the caller opt out of the per-CPU requirement. Affinity migration and retargeting on offline then follow the normal SPI path, which seems more natural to me.

I prefer Option 2. If it fits your series, I'll post the POC as an RFC for feedback.

Quoted 21 lines

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. [...] the same NMI handler could execute concurrently on two CPUs.

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. Consider:

CPU0 CPU1 CPU2

---------------------------------------------------------------------------

<NMI>

handling started
set NMI affinity to CPU2
<NMI> handling started
mutate shared state
mutate shared state
handling finished
handling finished

Quoted 3 lines

IOW, the same NMI handler could execute concurrently on two CPUs, potentially corrupting shared state. Once again more than happy to be corrected if I missed something :)

There is no risk of the same normal, non-per-CPU IRQ action handler executing concurrently on both the previous and newly selected CPUs during an affinity change. Genirq sets IRQD_IRQ_INPROGRESS before dropping the descriptor lock to invoke the driver's hard-IRQ handler. If another instance arrives on the new target while the handler remains active on the previous target, the new CPU observes that in-progress state and does not enter the action handler.

The affinity update is not a synchronous “wait for the old CPU” operation:
the interrupt controller can route a subsequent interrupt to the new CPU before the old handler returns. However, handler execution remains serialized:

Previous CPU New target CPU
------------ --------------
set IRQD_IRQ_INPROGRESS
drop desc->lock
run driver's hard-IRQ handler

affinity target is reprogrammed a subsequent IRQ arrives observes IRQD_IRQ_INPROGRESS does not run the action handler

handler returns
reacquire desc->lock
clear IRQD_IRQ_INPROGRESS

a later or replayed IRQ may run the action handler on the new CPU

Any required pending/replay behavior for an event arriving during this interval is handled by the applicable IRQ flow handler and irqchip semantics; it does not cause simultaneous invocations of the driver's hard-IRQ handler.

Does this make sense?

Cheers,
Ashok

author
#5 lore raw
Quoted 56 lines · Ashok Raj

Hi Vladimir

Quoted 21 lines

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. [...] the same NMI handler could execute concurrently on two CPUs.

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. Consider:

CPU0 CPU1 CPU2

---------------------------------------------------------------------------

<NMI>

handling started
set NMI affinity to CPU2
<NMI> handling started
mutate shared state
mutate shared state
handling finished
handling finished

Quoted 3 lines

IOW, the same NMI handler could execute concurrently on two CPUs, potentially corrupting shared state. Once again more than happy to be corrected if I missed something :)

There is no risk of the same normal, non-per-CPU IRQ action handler executing concurrently on both the previous and newly selected CPUs during an affinity change. Genirq sets IRQD_IRQ_INPROGRESS before dropping the descriptor lock to invoke the driver's hard-IRQ handler. If another instance arrives on the new target while the handler remains active on the previous target, the new CPU observes that in-progress state and does not enter the action handler.

The affinity update is not a synchronous “wait for the old CPU” operation:
the interrupt controller can route a subsequent interrupt to the new CPU before the old handler returns. However, handler execution remains serialized:

Previous CPU New target CPU
------------ --------------
set IRQD_IRQ_INPROGRESS
drop desc->lock
run driver's hard-IRQ handler

affinity target is reprogrammed a subsequent IRQ arrives observes IRQD_IRQ_INPROGRESS does not run the action handler

handler returns
reacquire desc->lock
clear IRQD_IRQ_INPROGRESS

Quoted 8 lines

a later or replayed IRQ may run the action handler on the new CPU

Any required pending/replay behavior for an event arriving during this interval is handled by the applicable IRQ flow handler and irqchip semantics; it does not cause simultaneous invocations of the driver's hard-IRQ handler.

Does this make sense?

I think what i quoted is true for regular interrupts, but NMI's there is no IRQD_IRQ_INPROGRESS serialization. So that overlap as you quote might be possible.

I'll need more time to stare at the code and rebase my RFC and post a coherent series that uses it to get the full picture.

Cheers,
Ashok

#6 lore raw
Quoted 65 lines · Ashok Raj
Quoted 63 lines · Ashok Raj

Hi Vladimir

Quoted 21 lines

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. [...] the same NMI handler could execute concurrently on two CPUs.

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. Consider:

CPU0 CPU1 CPU2

---------------------------------------------------------------------------

<NMI>

handling started
set NMI affinity to CPU2
<NMI> handling started
mutate shared state
mutate shared state
handling finished
handling finished

Quoted 3 lines

IOW, the same NMI handler could execute concurrently on two CPUs, potentially corrupting shared state. Once again more than happy to be corrected if I missed something :)

There is no risk of the same normal, non-per-CPU IRQ action handler executing concurrently on both the previous and newly selected CPUs during an affinity change. Genirq sets IRQD_IRQ_INPROGRESS before dropping the descriptor lock to invoke the driver's hard-IRQ handler. If another instance arrives on the new target while the handler remains active on the previous target, the new CPU observes that in-progress state and does not enter the action handler.

The affinity update is not a synchronous “wait for the old CPU” operation:
the interrupt controller can route a subsequent interrupt to the new CPU before the old handler returns. However, handler execution remains serialized:

Previous CPU New target CPU
------------ --------------
set IRQD_IRQ_INPROGRESS
drop desc->lock
run driver's hard-IRQ handler

affinity target is reprogrammed a subsequent IRQ arrives observes IRQD_IRQ_INPROGRESS does not run the action handler

handler returns
reacquire desc->lock
clear IRQD_IRQ_INPROGRESS

a later or replayed IRQ may run the action handler on the new CPU

Any required pending/replay behavior for an event arriving during this interval is handled by the applicable IRQ flow handler and irqchip semantics; it does not cause simultaneous invocations of the driver's hard-IRQ handler.

Quoted 8 lines
Quoted 1 line

Does this make sense?

I think what i quoted is true for regular interrupts, but NMI's there is no IRQD_IRQ_INPROGRESS serialization. So that overlap as you quote might be possible.

I'll need more time to stare at the code and rebase my RFC and post a coherent series that uses it to get the full picture.

When you do that, please don't mix functionalities and tests as you did in the first patch. Slap the tests at the end of the series instead, as separate patches.

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author
#7 lore raw
Quoted 5 lines · Marc Zyngier
Quoted 5 lines

I think what i quoted is true for regular interrupts, but NMI's there is no IRQD_IRQ_INPROGRESS serialization. So that overlap as you quote might be possible.

Quoted 7 lines
Quoted 2 lines

I'll need more time to stare at the code and rebase my RFC and post a coherent series that uses it to get the full picture.

When you do that, please don't mix functionalities and tests as you did in the first patch. Slap the tests at the end of the series instead, as separate patches.

Thanks for the tip. Certainly!

Signature · 3 lines
/ashok.raj
[email protected]
Qualcomm Inc
author
#8 lore raw
In reply to Vladimir Murzin

Hi Vladimir

Quoted 60 lines · Vladimir Murzin

Hi Ashok,

Quoted 28 lines · Ashok Raj

request_nmi() currently models NMIs as per-CPU sources. This does not represent a globally routed NMI source with an affinity-capable irqchip, which needs to be rerouted when its target CPU is offlined.

This RFC adds IRQF_NMI_MIGRATABLE as a request_nmi() registration mode for such a source. It remains excluded from normal IRQ balancing, but the generic startup and CPU-hotplug paths may select an online target. The second patch fixes the related /proc/interrupts display: NMI-safe flow handling updates the per-CPU counters rather than irq_desc::tot_count.

Patch 1: Add the migratable-NMI registration mode, validate that it is used only through request_nmi() with an affinity-capable non-per-CPU irqchip, and add IRQ KUnit coverage for registration and CPU-offline migration.

Patch 2: Emit the per-CPU counters for all NMI descriptors, matching kstat_irqs_desc().

Testing:
- ARM64 changed-object builds with WERROR=1
- ARM64 !SMP changed-object build
- ARM64 IRQ KUnit run under QEMU with two CPUs, including registration validation and CPU-offline migration
- local downstream GHES delivery and migration validation for the intended consumer topology (not part of this series)

Base and dependencies:

Quoted 8 lines

This RFC was developed and tested on v7.4-rc2 plus the FEAT_NMI v2 series [1]. It depends on that series' GIC NMI support and is therefore not directly applicable to mainline or linux-next as posted. This RFC is for design feedback; it will need rebasing onto the upstream form of those prerequisites before submission.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

I'm not an expert in this area, but based on what I've learned so far, NMI being per-CPU like makes life much easier ;)

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. Consider:

CPU0 CPU1 CPU2

---------------------------------------------------------------------------

<NMI>

handling started
set NMI affinity to CPU2
<NMI> handling started
mutate shared state
mutate shared state
handling finished
handling finished

Quoted 4 lines

IOW, the same NMI handler could execute concurrently on two CPUs, potentially corrupting shared state. Once again more than happy to be corrected if I missed something :)

Thanks, you're right, and I got it wrong in my earlier reply. genirq doesn't serialize NMI handlers: handle_fasteoi_nmi() takes no desc->lock and sets no IRQD_IRQ_INPROGRESS.

These interrupts are external interrupts in the platform, not Per CPU interrupts like the perf kinds.

I'm new to the GIC, so I'd like to check an assumption. On GICv3, an active SPI isn't signalled again until it is deactivated. handle_fasteoi_nmi() calls chip->irq_eoi() only after the handler returns.

So I'm assuming a second CPU can't be sent the same SPI while the first CPU is still in the handler.

Is that a reasonable assumption?

I didn't quite follow the mutate shared state comment.

I just interpreted thast as "handler is now called in another CPU" type assumption.

Cheers,
Ashok

#9 lore raw

Hi Ashok,

Sorry for delaying reply... got caught by sorting out Sashiko comments on FEAT_NMI :)

Quoted 62 lines · Ashok Raj

Hi Vladimir

Quoted 59 lines · Vladimir Murzin

Hi Ashok,

Quoted 28 lines · Ashok Raj

request_nmi() currently models NMIs as per-CPU sources. This does not represent a globally routed NMI source with an affinity-capable irqchip, which needs to be rerouted when its target CPU is offlined.

This RFC adds IRQF_NMI_MIGRATABLE as a request_nmi() registration mode for such a source. It remains excluded from normal IRQ balancing, but the generic startup and CPU-hotplug paths may select an online target. The second patch fixes the related /proc/interrupts display: NMI-safe flow handling updates the per-CPU counters rather than irq_desc::tot_count.

Patch 1: Add the migratable-NMI registration mode, validate that it is used only through request_nmi() with an affinity-capable non-per-CPU irqchip, and add IRQ KUnit coverage for registration and CPU-offline migration.

Patch 2: Emit the per-CPU counters for all NMI descriptors, matching kstat_irqs_desc().

Testing:
- ARM64 changed-object builds with WERROR=1
- ARM64 !SMP changed-object build
- ARM64 IRQ KUnit run under QEMU with two CPUs, including registration validation and CPU-offline migration
- local downstream GHES delivery and migration validation for the intended consumer topology (not part of this series)

Base and dependencies:

Quoted 8 lines

This RFC was developed and tested on v7.4-rc2 plus the FEAT_NMI v2 series [1]. It depends on that series' GIC NMI support and is therefore not directly applicable to mainline or linux-next as posted. This RFC is for design feedback; it will need rebasing onto the upstream form of those prerequisites before submission.

[1] https://lore.kernel.org/linux-arm-kernel/[email protected]/

I'm not an expert in this area, but based on what I've learned so far, NMI being per-CPU like makes life much easier ;)

IIUC, one of the main problems with a shared NMI (as opposed to a per-CPU) is handling shared state. Consider:

CPU0 CPU1 CPU2

---------------------------------------------------------------------------

<NMI>

handling started
set NMI affinity to CPU2
<NMI> handling started
mutate shared state
mutate shared state
handling finished
handling finished

Quoted 8 lines
Quoted 4 lines

IOW, the same NMI handler could execute concurrently on two CPUs, potentially corrupting shared state. Once again more than happy to be corrected if I missed something :)

Thanks, you're right, and I got it wrong in my earlier reply. genirq doesn't serialize NMI handlers: handle_fasteoi_nmi() takes no desc->lock and sets no IRQD_IRQ_INPROGRESS.

Matches my observation.

Quoted 3 lines

These interrupts are external interrupts in the platform, not Per CPU interrupts like the perf kinds.

Correct, that why I call them per-CPU like :)

Quoted 5 lines

I'm new to the GIC, so I'd like to check an assumption. On GICv3, an active SPI isn't signalled again until it is deactivated. handle_fasteoi_nmi() calls chip->irq_eoi() only after the handler returns.

Yes.

Quoted 4 lines

So I'm assuming a second CPU can't be sent the same SPI while the first CPU is still in the handler.

Is that a reasonable assumption?

Perhaps. I'm still trying to understand where is border between what we can assume and what we must guaranty, especially when it comes to generic implementation.

Quoted 3 lines

I didn't quite follow the mutate shared state comment.

That's generic statement since NMI flow doesn't take locks. I still need to understand what and why IRQ flow protects with locks and how it relates to NMI flow... Sorry for making such vague statements!

Ideally, for generic support for global NMIs (opposite to per-CPU, I do not use term "shared" since it confuses people with IRQ_SHARED which is not in scope here!) we want requestor just omit IRQF_PERCPU and the generic flow do the right thing automatically. That requires enumerating corer cases due to NMI is not per-CPU anymore and providing measures to either avoid those cases or handle them or likely balance between both. I'm spending some spare cycles on that, but things go slowly...

Cheers
Vladimir

Quoted 6 lines

I just interpreted thast as "handler is now called in another CPU" type assumption.

Cheers,
Ashok