Skip to contentSkip to conversation

x86: watchdog/hardlockup/hpet: Add support for interrupt remapping

RFC PATCH In review Ricardo Neri 2 participants 11 messages May 4, 2021 – May 14, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v5 cover x86: watchdog/hardlockup/hpet: Add support for interrupt remapping

Hi IOMMU experts,

I proposed a hardlockup detector driven by the HPET timer [1]. Such detector is driven by a single timer. The hardlockup detector brings the extra complexity of having to update the affinity of the interrupt periodically and initiated from NMI context. The proposed design only requires updating the affinity every watchdog_thresh (the interval is between [1, 60] seconds). Also, the affinity update is offloaded to an irq_work. Handling the HPET interrupt affinity is trivial with !intremap since the detector composes the MSI message and writes it directly to the HPET registers.

However, for intremap we must use the existing IOMMU drivers as well as the kernel's irq plumbing. Thomas Gleixner has imposed two restrictions:
1) Do not implement an IRQF_NMI flag for x86 as it is not possible to determine the source of an NMI [2].
2) Use the irq subsystem to update the affinity of the HPET

     interrupt [3].

1) implies that the interrupt remapping drivers need to implement a quirk to identify the HPET interrupt and update its delivery mode to NMI. 2) means that the hardlockup detector must use request_irq() to allocate the HPET interrupt.

This patch series attempts to meet the requirements above by
a) Decoupling the delivery mode of an APIC interrupt from the delivery mode of the APIC driver (patch 1)
b) Implement quirks in the Intel and AMD IOMMU drivers to identify the HPET timer and update the delivery mode accordingly (patches 2-5).
c) Add support for interrupt remapping in the HPET hardlockup detector in [1]. This includes the unavoidable eyesore of using request_irq() and having a useless regular interrupt handler (patch 6).

I would like to get your feedback on whether the HPET NMI quirk looks sane to you and whether offloading the affinity setup to an irq_work could pose issues.

Thanks and BR,
Ricardo

[1]. https://lore.kernel.org/lkml/[email protected]/T/#mf77988cc98f9ca6988831e17f68394577388959d
[2]. https://lore.kernel.org/lkml/[email protected]/
[3]. https://lore.kernel.org/lkml/[email protected]/

Changes since v4:
* With !CONFIG_IRQ_REMAP [1] now disables the HPET channel before changing the MSI Destination ID field. This should avoid races between a pending interrupt and updating the detector's interrupt affinity. (Ashok)
* Rebased to use new enumeration apic_delivery_modes.
* Removed custom functions to allocate an interrupt for the detector and instead added support to identify the detector's interrupt and change the delivery mode.
* With interrupt remapping enabled, use request_irq().
* Added support for AMD IOMMU.

Changes since v3:
* None

Changes since v2:
* None

Changes since v1:
* Introduced support for interrupt remapping

Ricardo Neri (7):
x86/apic: Add irq_cfg::delivery_mode
x86/hpet: Introduce function to identify HPET hardlockup detector irq iommu/vt-d: Rework prepare_irte() to support per-irq delivery mode iommu/amd: Set the IRTE delivery mode from irq_cfg iommu/vt-d: Fixup delivery mode of the HPET hardlockup interrupt iommu/amd: Fixup delivery mode of the HPET hardlockup interrupt x86/watchdog/hardlockup/hpet: Support interrupt remapping

 arch/x86/include/asm/hpet.h         |  5 +++
 arch/x86/include/asm/hw_irq.h       |  1 +
 arch/x86/kernel/apic/vector.c       | 10 ++++++
 arch/x86/kernel/hpet.c              | 36 ++++++++++++++++++++++
 arch/x86/kernel/watchdog_hld_hpet.c | 48 +++++++++++++++++++++++++----
 drivers/iommu/amd/iommu.c           | 11 ++++++-
 drivers/iommu/intel/irq_remapping.c | 20 ++++++++----
 7 files changed, 118 insertions(+), 13 deletions(-)
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#1 lore raw

RFC PATCH v5 1/7 x86/apic: Add irq_cfg::delivery_mode

Until now, the delivery mode of APIC interrupts is set to the default mode set in the APIC driver. However, there are no restrictions in hardware to configure each interrupt with a different delivery mode. Specifying the delivery mode per interrupt is useful when one is interested in changing the delivery mode of a particular interrupt. For instance, this can be used to deliver an interrupt as non-maskable.

Add a new member, delivery_mode, to struct irq_cfg. This new member can be used to update the configuration of the delivery mode in each interrupt domain.

Currently, all interrupt domains set the delivery mode of interrupts using the APIC setting. Interrupt domains use an irq_cfg data structure to configure their own data structures and hardware resources. Thus, in order to keep the current behavior, set the delivery mode of the irq configuration that as the APIC setting. In this manner, irq domains can obtain the delivery mode from the irq configuration data instead of the APIC setting, if needed.

Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* Rebased to use new enumeration apic_delivery_modes.

Changes since v3:
* None

Changes since v2:
* Reduced scope to only add the interrupt delivery mode in struct irq_alloc_info.

Changes since v1:
* Introduced this patch.

---
 arch/x86/include/asm/hw_irq.h |  1 +
 arch/x86/kernel/apic/vector.c | 10 ++++++++++
 2 files changed, 11 insertions(+)
arch/x86/include/asm/hw_irq.h +1 -0
index d465ece58151..370f4db0372b 100644
@@ -90,6 +90,7 @@ struct irq_alloc_info {
 struct irq_cfg {
 	unsigned int		dest_apicid;
 	unsigned int		vector;
+	enum apic_delivery_modes	delivery_mode;
 };
 
 extern struct irq_cfg *irq_cfg(unsigned int irq);
arch/x86/kernel/apic/vector.c +10 -0
index 6dbdc7c22bb7..d47ed07a56a4 100644
@@ -567,6 +567,16 @@ static int x86_vector_alloc_irqs(struct irq_domain *domain, unsigned int virq,
 		irqd->chip_data = apicd;
 		irqd->hwirq = virq + i;
 		irqd_set_single_target(irqd);
+
+		/*
+		 * Initialize the delivery mode of this irq to match the
+		 * default delivery mode of the APIC. This is useful for
+		 * children irq domains which want to take the delivery
+		 * mode from the individual irq configuration rather
+		 * than from the APIC.
+		 */
+		 apicd->hw_irq_cfg.delivery_mode = apic->delivery_mode;
+
 		/*
 		 * Prevent that any of these interrupts is invoked in
 		 * non interrupt context via e.g. generic_handle_irq()
Signature · 1 line
2.17.1
#2 lore raw

RFC PATCH v5 2/7 x86/hpet: Introduce function to identify HPET hardlockup detector irq

In reply to Ricardo Neri

The HPET hardlockup detector needs to deliver its interrupt as NMI. In x86 there is not an IRQF_NMI flag that can be used in the irq plumbing code to tell interrupt remapping drivers to set the interrupt delivery mode accordingly. Hence, they must fixup the delivery mode internally.

Implement a method to determine if the interrupt being allocated belongs to the HPET hardlockup detector.

Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* Introduced this patch. Previous versions had special functions to allocate and set the affinity of a remapped NMI interrupt.

Changes since v3:
* N/A

Changes since v2:
* N/A

Changes since v1:
* N/A

---
 arch/x86/include/asm/hpet.h |  3 +++
 arch/x86/kernel/hpet.c      | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
arch/x86/include/asm/hpet.h +3 -0
index df11c7d4af44..5bf675970d4b 100644
@@ -149,6 +149,7 @@ extern void hardlockup_detector_hpet_stop(void);
 extern void hardlockup_detector_hpet_enable(unsigned int cpu);
 extern void hardlockup_detector_hpet_disable(unsigned int cpu);
 extern void hardlockup_detector_switch_to_perf(void);
+extern bool is_hpet_irq_hardlockup_detector(struct irq_alloc_info *info);
 #else
 static inline int hardlockup_detector_hpet_init(void)
 { return -ENODEV; }
@@ -156,6 +157,8 @@ static inline void hardlockup_detector_hpet_stop(void) {}
 static inline void hardlockup_detector_hpet_enable(unsigned int cpu) {}
 static inline void hardlockup_detector_hpet_disable(unsigned int cpu) {}
 static inline void hardlockup_detector_switch_to_perf(void) {}
+static inline bool is_hpet_irq_hardlockup_detector(struct irq_alloc_info *info)
+{ return false; }
 #endif /* CONFIG_X86_HARDLOCKUP_DETECTOR_HPET */
 
 #else /* CONFIG_HPET_TIMER */
arch/x86/kernel/hpet.c +33 -0
index 5012590dc1b8..3e43e0f348b8 100644
@@ -1479,6 +1479,39 @@ struct hpet_hld_data *hpet_hld_get_timer(void)
 	hld_data = NULL;
 	return NULL;
 }
+
+/**
+ * is_hpet_irq_hardlockup_detector() - Identify the HPET hld interrupt info
+ * @info:	Interrupt allocation info, with private HPET channel data
+ *
+ * The HPET hardlockup detector is special as it needs its interrupts delivered
+ * as NMI. However, for interrupt remapping we use the existing irq subsystem
+ * to configure and route the HPET interrupt. Unfortunately, there is not a
+ * IRQF_NMI flag for x86. Instead, identify whether the interrupt being
+ * allocated for the HPET channel belongs to the hardlockup detector.
+ *
+ * Returns: True if @info indicates that it belongs to the HPET hardlockup
+ * detector. False otherwise.
+ */
+bool is_hpet_irq_hardlockup_detector(struct irq_alloc_info *info)
+{
+	struct hpet_channel *hc;
+
+	if (!info)
+		return false;
+
+	if (info->type != X86_IRQ_ALLOC_TYPE_HPET)
+		return false;
+
+	hc = info->data;
+	if (!hc)
+		return false;
+
+	if (hc->mode == HPET_MODE_NMI_WATCHDOG)
+		return true;
+
+	return false;
+}
 #endif /* CONFIG_X86_HARDLOCKUP_DETECTOR_HPET */
 
 #endif
Signature · 1 line
2.17.1
#3 lore raw

RFC PATCH v5 3/7 iommu/vt-d: Rework prepare_irte() to support per-irq delivery mode

In reply to Ricardo Neri

A previous changeset introduced a new member to struct irq_cfg to specify the delivery mode of an interrupt. Supporting the configuration of the delivery mode would require adding a third argument to prepare_irte(). Instead, simply take a pointer to an irq_cfg data structure as the only argument.

Always configure the delivery mode of the Interrupt Remapping Table Entry using the values specified in the irq_cfg data structure.

This change does not change the existing behavior, as the delivery mode of the APIC is used to configure the irq_cfg of each irq.

Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* None

Changes since v3:
* None

Changes since v2:
* None

Changes since v1:
* Introduced this patch.

---
 drivers/iommu/intel/irq_remapping.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/iommu/intel/irq_remapping.c +5 -6
index 611ef5243cb6..daa5df53db59 100644
@@ -1104,7 +1104,7 @@ void intel_irq_remap_add_device(struct dmar_pci_notify_info *info)
 	dev_set_msi_domain(&info->dev->dev, map_dev_to_ir(info->dev));
 }
 
-static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
+static void prepare_irte(struct irte *irte, struct irq_cfg *irq_cfg)
 {
 	memset(irte, 0, sizeof(*irte));
 
@@ -1118,9 +1118,9 @@ static void prepare_irte(struct irte *irte, int vector, unsigned int dest)
 	 * irq migration in the presence of interrupt-remapping.
 	*/
 	irte->trigger_mode = 0;
-	irte->dlvry_mode = apic->delivery_mode;
-	irte->vector = vector;
-	irte->dest_id = IRTE_DEST(dest);
+	irte->dlvry_mode = irq_cfg->delivery_mode;
+	irte->vector = irq_cfg->vector;
+	irte->dest_id = IRTE_DEST(irq_cfg->dest_apicid);
 	irte->redir_hint = 1;
 }
 
@@ -1261,8 +1261,7 @@ static void intel_irq_remapping_prepare_irte(struct intel_ir_data *data,
 {
 	struct irte *irte = &data->irte_entry;
 
-	prepare_irte(irte, irq_cfg->vector, irq_cfg->dest_apicid);
-
+	prepare_irte(irte, irq_cfg);
 	switch (info->type) {
 	case X86_IRQ_ALLOC_TYPE_IOAPIC:
 		/* Set source-id of interrupt request */
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#4 lore raw

RFC PATCH v5 4/7 iommu/amd: Set the IRTE delivery mode from irq_cfg

In reply to Ricardo Neri

There is not hardware requirement to have a different delivery mode for each interrupt. Instead of using the delivery mode of the APIC driver, use the delivery mode of each specific interrupt configuration.

This allows to accommodate interrupts which require a specific delivery mode, such as the HPET hardlockup detector.

Outside of such case, there are not functional changes since the delivery mode of an interrupt is initialized with the delivery mode of the APIC driver.

Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* Introduced this patch.

Changes since v3:
* N/A

Changes since v2:
* N/A

Changes since v1:
* N/A

---
 drivers/iommu/amd/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +1 -1
index a69a8b573e40..e8d9fae0c766 100644
@@ -3122,7 +3122,7 @@ static void irq_remapping_prepare_irte(struct amd_ir_data *data,
 
 	data->irq_2_irte.devid = devid;
 	data->irq_2_irte.index = index + sub_handle;
-	iommu->irte_ops->prepare(data->entry, apic->delivery_mode,
+	iommu->irte_ops->prepare(data->entry, irq_cfg->delivery_mode,
 				 apic->dest_mode_logical, irq_cfg->vector,
 				 irq_cfg->dest_apicid, devid);
 
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#5 lore raw

RFC PATCH v5 5/7 iommu/vt-d: Fixup delivery mode of the HPET hardlockup interrupt

In reply to Ricardo Neri

The HPET hardlockup detector requires that the HPET timer delivers the interrupt as NMI. When interrupt remapping is disabled, this can be done by programming the HPET MSI registers directly. With interrupt remapping, it is necessary to populate an entry in the interrupt remapping table.

In x86 there is not an IRQF_NMI flag that can be used to indicate the delivery mode when requesting an interrupt (via request_irq()). Thus, there is no way for the interrupt remapping driver to know and set the delivery mode.

Hence, when allocating an interrupt, check if such interrupt belongs to the HPET hardlockup detector and fixup the delivery mode accordingly.

Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* Introduced this patch.

Changes since v3:
* N/A

Changes since v2:
* N/A

Changes since v1:
* N/A

---
 drivers/iommu/intel/irq_remapping.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/iommu/intel/irq_remapping.c +9 -0
index daa5df53db59..b07c68ecac01 100644
@@ -18,6 +18,7 @@
 #include <asm/apic.h>
 #include <asm/smp.h>
 #include <asm/cpu.h>
+#include <asm/hpet.h>
 #include <asm/irq_remapping.h>
 #include <asm/pci-direct.h>
 
@@ -1376,6 +1377,14 @@ static int intel_irq_remapping_alloc(struct irq_domain *domain,
 		irq_data->hwirq = (index << 16) + i;
 		irq_data->chip_data = ird;
 		irq_data->chip = &intel_ir_chip;
+
+		/*
+		 * If we find the HPET hardlockup detector irq, fixup the
+		 * delivery mode.
+		 */
+		if (is_hpet_irq_hardlockup_detector(info))
+			irq_cfg->delivery_mode = APIC_DELIVERY_MODE_NMI;
+
 		intel_irq_remapping_prepare_irte(ird, irq_cfg, info, index, i);
 		irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
 	}
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#6 lore raw

Re: [RFC PATCH v5 5/7] iommu/vt-d: Fixup delivery mode of the HPET hardlockup interrupt

Quoted 1 line · Ricardo Neri

In x86 there is not an IRQF_NMI flag that can be used to indicate the

There exists no IRQF_NMI flag at all. No architecture provides that.

Quoted 3 lines

delivery mode when requesting an interrupt (via request_irq()). Thus, there is no way for the interrupt remapping driver to know and set the delivery mode.

There is no support for this today. So what?

Quoted 2 lines

Hence, when allocating an interrupt, check if such interrupt belongs to the HPET hardlockup detector and fixup the delivery mode accordingly.

What?

drivers/iommu/intel/irq_remapping.c static int intel_irq_remapping_alloc(struct irq_domain *domain, · line 1386

Quoted 6 lines
drivers/iommu/intel/irq_remapping.c +6 -0
+		/*
+		 * If we find the HPET hardlockup detector irq, fixup the
+		 * delivery mode.
+		 */
+		if (is_hpet_irq_hardlockup_detector(info))
+			irq_cfg->delivery_mode = APIC_DELIVERY_MODE_NMI;

Again. We are not sticking some random device checks into that code. It's wrong and I explained it to you before.

https://lore.kernel.org/lkml/[email protected]/

But I'm happy to repeat it again:

"No. This is horrible hackery violating all the layering which we carefully
put into place to avoid exactly this kind of sprinkling conditionals into all code pathes.

With some thought the existing irqdomain hierarchy can be used to achieve the same thing without tons of extra functions and conditionals."

So the outcome of thought and using the irqdomain hierarchy is:

Replacing an hpet specific conditional in one place with an hpet specific conditional in a different place.

Impressive.

hpet_assign_irq(...., bool nmi)
init_info(info)

    ...
    if (nmi)
        info.flags |= X86_IRQ_ALLOC_AS_NMI;
   irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info)
     intel_irq_remapping_alloc(..., info)
       irq_domain_alloc_irq_parents(..., info)
         x86_vector_alloc_irqs(..., info)
         {   
           if (info->flags & X86_IRQ_ALLOC_AS_NMI && nr_irqs != 1)
                  return -EINVAL;
           for (i = 0; i < nr_irqs; i++) {
             ....
             if (info->flags & X86_IRQ_ALLOC_AS_NMI) {
                 irq_cfg_setup_nmi(apicd);
                 continue;
             }
             ...
         }

irq_cfg_setup_nmi() sets irq_cfg->delivery_mode and whatever is required and everything else just works. Of course this needs a few other minor tweaks but none of those introduces random hpet quirks all over the place. Not convoluted enough, right?

But that solves none of other problems. Let me summarize again which options or non-options we have:

1) Selective IPIs from NMI context cannot work

As explained in the other thread.

2) Shorthand IPI allbutself from NMI

This should work, but that obviously does not take the watchdog cpumask into account.

Also this only works when IPI shorthand mode is enabled. See apic_smt_update() for details.

3) Sending the IPIs from irq_work

This would solve the problem, but if the CPU which is the NMI target is really stuck in an interrupt disabled region then the IPIs won't be sent.

OTOH, if that's the case then the CPU which was processing the NMI will continue to be stuck until the next NMI hits which will detect that the CPU is stuck which is a good enough reason to send a shorthand IPI to all CPUs ignoring the watchdog cpumask.

Same limitation vs. shorthand mode as #2

4) Changing affinity of the HPET NMI from NMI

As we established two years ago that cannot work with interrupt remapping

5) Changing affinity of the HPET NMI from irq_work

Same issues as #3

Anything else than #2 is just causing more problems than it solves, but surely the NOHZ_FULL/isolation people might have opinions on this.

OTOH, as this is opt-in, anything which wants a watchdog mask which is not the full online set, has to accept that HPET has these restrictions.

And that's exactly what I suggested two years ago:

https://lore.kernel.org/lkml/[email protected]/

"It definitely would be worthwhile to experiment with that. if we
could use shorthands (also for regular IPIs) that would be a great improvement in general and would nicely solve that NMI issue. Beware of the dragons though."

As a consequence of this conversation I implemented shorthand IPIs...

But I haven't seen any mentioning that this has been tried, why the approach was not chosen or any discussion about that matter.

Not that I'm surprised.

Thanks,

tglx
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu

#7 lore raw

Re: [RFC PATCH v5 5/7] iommu/vt-d: Fixup delivery mode of the HPET hardlockup interrupt

Quoted 1 line · Thomas Gleixner

On Tue, May 04 2021 at 12:10, Ricardo Neri wrote:

Thank you very much for your feedback, Thomas. I am sorry it took me a while to reply to your email. I needed to digest and research your comments.

Quoted 3 lines
Quoted 1 line

In x86 there is not an IRQF_NMI flag that can be used to indicate the

There exists no IRQF_NMI flag at all. No architecture provides that.

Thank you for the clarification. I think I meant to say that there is a request_nmi() function but AFAIK it is only used in the ARM PMU and would not work on x86.

Quoted 6 lines
Quoted 4 lines

delivery mode when requesting an interrupt (via request_irq()). Thus, there is no way for the interrupt remapping driver to know and set the delivery mode.

There is no support for this today. So what?

Using request_irq() plus a HPET quirk looked to me a reasonable way to use the irqdomain hierarchy to allocate an interrupt with NMI as the delivery mode.

Quoted 27 lines
Quoted 3 lines

Hence, when allocating an interrupt, check if such interrupt belongs to the HPET hardlockup detector and fixup the delivery mode accordingly.

What?

drivers/iommu/intel/irq_remapping.c static int intel_irq_remapping_alloc(struct irq_domain *domain, · line 1386

Quoted 6 lines
drivers/iommu/intel/irq_remapping.c +6 -0
+		/*
+		 * If we find the HPET hardlockup detector irq, fixup the
+		 * delivery mode.
+		 */
+		if (is_hpet_irq_hardlockup_detector(info))
+			irq_cfg->delivery_mode = APIC_DELIVERY_MODE_NMI;

Again. We are not sticking some random device checks into that code. It's wrong and I explained it to you before.

https://lore.kernel.org/lkml/[email protected]/

But I'm happy to repeat it again:

"No. This is horrible hackery violating all the layering which we carefully
put into place to avoid exactly this kind of sprinkling conditionals into all code pathes.

With some thought the existing irqdomain hierarchy can be used to achieve the same thing without tons of extra functions and conditionals."

Quoted 6 lines

So the outcome of thought and using the irqdomain hierarchy is:

Replacing an hpet specific conditional in one place with an hpet specific conditional in a different place.

Impressive.

I am sorry Thomas, I did try to make the quirk less hacky but I did not think of the solution you provide below.

Quoted 24 lines

hpet_assign_irq(...., bool nmi)
init_info(info)

    ...
    if (nmi)
        info.flags |= X86_IRQ_ALLOC_AS_NMI;
   irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info)
     intel_irq_remapping_alloc(..., info)
       irq_domain_alloc_irq_parents(..., info)
         x86_vector_alloc_irqs(..., info)
         {   
           if (info->flags & X86_IRQ_ALLOC_AS_NMI && nr_irqs != 1)
                  return -EINVAL;
           for (i = 0; i < nr_irqs; i++) {
             ....
             if (info->flags & X86_IRQ_ALLOC_AS_NMI) {
                 irq_cfg_setup_nmi(apicd);
                 continue;
             }
             ...
         }
Quoted 4 lines

irq_cfg_setup_nmi() sets irq_cfg->delivery_mode and whatever is required and everything else just works. Of course this needs a few other minor tweaks but none of those introduces random hpet quirks all over the place. Not convoluted enough, right?

Thanks for the detailed demonstration! It does seem cleaner than what I implemented.

Quoted 54 lines

But that solves none of other problems. Let me summarize again which options or non-options we have:

1) Selective IPIs from NMI context cannot work

As explained in the other thread.

2) Shorthand IPI allbutself from NMI

This should work, but that obviously does not take the watchdog cpumask into account.

Also this only works when IPI shorthand mode is enabled. See apic_smt_update() for details.

3) Sending the IPIs from irq_work

This would solve the problem, but if the CPU which is the NMI target is really stuck in an interrupt disabled region then the IPIs won't be sent.

OTOH, if that's the case then the CPU which was processing the NMI will continue to be stuck until the next NMI hits which will detect that the CPU is stuck which is a good enough reason to send a shorthand IPI to all CPUs ignoring the watchdog cpumask.

Same limitation vs. shorthand mode as #2

4) Changing affinity of the HPET NMI from NMI

As we established two years ago that cannot work with interrupt remapping

5) Changing affinity of the HPET NMI from irq_work

Same issues as #3

Anything else than #2 is just causing more problems than it solves, but surely the NOHZ_FULL/isolation people might have opinions on this.

OTOH, as this is opt-in, anything which wants a watchdog mask which is not the full online set, has to accept that HPET has these restrictions.

And that's exactly what I suggested two years ago:

https://lore.kernel.org/lkml/[email protected]/

"It definitely would be worthwhile to experiment with that. if we
could use shorthands (also for regular IPIs) that would be a great improvement in general and would nicely solve that NMI issue. Beware of the dragons though."

Quoted 4 lines

As a consequence of this conversation I implemented shorthand IPIs...

But I haven't seen any mentioning that this has been tried, why the approach was not chosen or any discussion about that matter.

Indeed, I focused on 5) and I overlooked your comment on using your new support for shortand IPIs.

I'll go back and see to implement option #2, or perhaps the alternative solution you proposed on a separate thread.

Thanks and BR,
Ricardo

#8 lore raw

Re: [RFC PATCH v5 5/7] iommu/vt-d: Fixup delivery mode of the HPET hardlockup interrupt

In reply to Ricardo Neri

On Tue, May 04 2021 at 12:10, Ricardo Neri wrote:

Resending as the original one did not make it on the list because of fatfingers. Sorry for the noise.

Quoted 1 line

In x86 there is not an IRQF_NMI flag that can be used to indicate the

There exists no IRQF_NMI flag at all. No architecture provides that.

Quoted 3 lines

delivery mode when requesting an interrupt (via request_irq()). Thus, there is no way for the interrupt remapping driver to know and set the delivery mode.

There is no support for this today. So what?

Quoted 2 lines

Hence, when allocating an interrupt, check if such interrupt belongs to the HPET hardlockup detector and fixup the delivery mode accordingly.

What?

drivers/iommu/intel/irq_remapping.c static int intel_irq_remapping_alloc(struct irq_domain *domain, · line 1386

Quoted 6 lines
drivers/iommu/intel/irq_remapping.c +6 -0
+		/*
+		 * If we find the HPET hardlockup detector irq, fixup the
+		 * delivery mode.
+		 */
+		if (is_hpet_irq_hardlockup_detector(info))
+			irq_cfg->delivery_mode = APIC_DELIVERY_MODE_NMI;

Again. We are not sticking some random device checks into that code. It's wrong and I explained it to you before.

https://lore.kernel.org/lkml/[email protected]/

But I'm happy to repeat it again:

"No. This is horrible hackery violating all the layering which we carefully
put into place to avoid exactly this kind of sprinkling conditionals into all code pathes.

With some thought the existing irqdomain hierarchy can be used to achieve the same thing without tons of extra functions and conditionals."

So the outcome of thought and using the irqdomain hierarchy is:

Replacing an hpet specific conditional in one place with an hpet specific conditional in a different place.

Impressive.

hpet_assign_irq(...., bool nmi)
init_info(info)

    ...
    if (nmi)
        info.flags |= X86_IRQ_ALLOC_AS_NMI;
   irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info)
     intel_irq_remapping_alloc(..., info)
       irq_domain_alloc_irq_parents(..., info)
         x86_vector_alloc_irqs(..., info)
         {   
           if (info->flags & X86_IRQ_ALLOC_AS_NMI && nr_irqs != 1)
      	    return -EINVAL;
           for (i = 0; i < nr_irqs; i++) {
             ....
             if (info->flags & X86_IRQ_ALLOC_AS_NMI) {
      	   irq_cfg_setup_nmi(apicd);
      	   continue;
             }
             ...
         }

irq_cfg_setup_nmi() sets irq_cfg->delivery_mode and whatever is required and everything else just works. Of course this needs a few other minor tweaks but none of those introduces random hpet quirks all over the place. Not convoluted enough, right?

But that solves none of other problems. Let me summarize again which options or non-options we have:

1) Selective IPIs from NMI context cannot work

As explained in the other thread.

2) Shorthand IPI allbutself from NMI

This should work, but that obviously does not take the watchdog cpumask into account.

Also this only works when IPI shorthand mode is enabled. See apic_smt_update() for details.

3) Sending the IPIs from irq_work

This would solve the problem, but if the CPU which is the NMI target is really stuck in an interrupt disabled region then the IPIs won't be sent.

OTOH, if that's the case then the CPU which was processing the NMI will continue to be stuck until the next NMI hits which will detect that the CPU is stuck which is a good enough reason to send a shorthand IPI to all CPUs ignoring the watchdog cpumask.

Same limitation vs. shorthand mode as #2

4) Changing affinity of the HPET NMI from NMI

As we established two years ago that cannot work with interrupt remapping

5) Changing affinity of the HPET NMI from irq_work

Same issues as #3

Anything else than #2 is just causing more problems than it solves, but surely the NOHZ_FULL/isolation people might have opinions on this.

OTOH, as this is opt-in, anything which wants a watchdog mask which is not the full online set, has to accept that HPET has these restrictions.

And that's exactly what I suggested two years ago:

https://lore.kernel.org/lkml/[email protected]/

"It definitely would be worthwhile to experiment with that. if we
could use shorthands (also for regular IPIs) that would be a great improvement in general and would nicely solve that NMI issue. Beware of the dragons though."

As a consequence of this conversation I implemented shorthand IPIs...

But I haven't seen any mentioning that this has been tried, why the approach was not chosen or any discussion about that matter.

Not that I'm surprised.

Thanks,

tglx

#9 lore raw

RFC PATCH v5 6/7 iommu/amd: Fixup delivery mode of the HPET hardlockup interrupt

In reply to Ricardo Neri

The HPET hardlockup detector requires that the HPET timer delivers the interrupt as NMI. When interrupt remapping is disabled, this can be done by programming the HPET MSI registers directly. With interrupt remapping, it is necessary to populate an entry in the interrupt remapping table.

In x86 there is not an IRQF_NMI flag that can be used to indicate the delivery mode when requesting an interrupt (via request_irq()). Thus, there is no way for the interrupt remapping driver to know and set the delivery mode.

Hence, when allocating an interrupt, check if such interrupt belongs to the HPET hardlockup detector and fixup the delivery mode accordingly.

Cc Ashok Raj <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* Introduced this patch.

Changes since v3:
* N/A

Changes since v2:
* N/A

Changes since v1:
* N/A

---
 drivers/iommu/amd/iommu.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/iommu/amd/iommu.c +9 -0
index e8d9fae0c766..758e08ba42e6 100644
@@ -35,6 +35,7 @@
 #include <asm/irq_remapping.h>
 #include <asm/io_apic.h>
 #include <asm/apic.h>
+#include <asm/hpet.h>
 #include <asm/hw_irq.h>
 #include <asm/proto.h>
 #include <asm/iommu.h>
@@ -3254,6 +3255,14 @@ static int irq_remapping_alloc(struct irq_domain *domain, unsigned int virq,
 		irq_data->hwirq = (devid << 16) + i;
 		irq_data->chip_data = data;
 		irq_data->chip = &amd_ir_chip;
+
+		/*
+		 * If we find the HPET hardlockup detector irq, fixup the
+		 * delivery mode.
+		 */
+		if (is_hpet_irq_hardlockup_detector(info))
+			cfg->delivery_mode = APIC_DELIVERY_MODE_NMI;
+
 		irq_remapping_prepare_irte(data, cfg, info, devid, index, i);
 		irq_set_status_flags(virq + i, IRQ_MOVE_PCNTXT);
 	}
Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#10 lore raw

RFC PATCH v5 7/7 x86/watchdog/hardlockup/hpet: Support interrupt remapping

In reply to Ricardo Neri

When interrupt remapping is enabled in the system, the MSI interrupt address and data fields must follow a special format that the IOMMU defines.

However, the HPET hardlockup detector must rely on the interrupt subsystem to have the interrupt remapping drivers allocate, activate, and set the affinity of HPET timer interrupt. Hence, it must use request_irq() to use such functionality.

In x86 there is not an IRQF_NMI flag to indicate to the interrupt subsystem the delivery mode of the interrupt. A previous changset added functionality to detect the interrupt of the HPET hardlockup detector and fixup the delivery mode accordingly.

Also, since request_irq() is used, a non-NMI interrupt handler must be defined. Even if it is not needed.

When Interrupt Remapping is enabled, use the new facility to ensure interrupt is plumbed properly to work with interrupt remapping.

Cc Ashok Raj <[email protected]>
Cc Andi Kleen <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc David Woodhouse <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc "Ravi V. Shankar" <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jacob Pan <[email protected]>
Cc Lu Baolu <[email protected]> (supporter:INTEL IOMMU (VT-d))
Cc Stephane Eranian <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc [email protected] (open list:INTEL IOMMU (VT-d))

---
Changes since v4:
* Use request_irq() to obtain an IRTE for the HPET hardlockup detector instead of the custom interfaces previously implemented in the interrupt remapping drivers.
* Simplified detection of interrupt remapping by checking the parent of the HPET irq domain.
* Stopped using the HPET magic fields of struct irq_alloc_info. They were removed in commit 2bf1e7bcedb8 ("x86/msi: Consolidate HPET allocation")
* Rephrased commit message for clarity. (Ashok)
* Clarified error message of non-NMI handler. (Ashok)

Changes since v3:
* None

Changes since v2:
* None

Changes since v1:
* Introduced this patch. Added custom functions in the Intel IOMMU driver to allocate an IRTE for the HPET hardlockup detector.

---
 arch/x86/include/asm/hpet.h         |  2 ++
 arch/x86/kernel/hpet.c              |  3 ++
 arch/x86/kernel/watchdog_hld_hpet.c | 48 +++++++++++++++++++++++++----
 3 files changed, 47 insertions(+), 6 deletions(-)
arch/x86/include/asm/hpet.h +2 -0
index 5bf675970d4b..d130285ddc96 100644
@@ -109,6 +109,7 @@ extern void hpet_unregister_irq_handler(rtc_irq_handler handler);
  * @tsc_ticks_per_group:	TSC ticks that must elapse for each group of
  *				monitored CPUs.
  * @irq:			IRQ number assigned to the HPET channel
+ * @int_remap_enabled:		True if interrupt remapping is enabled
  * @handling_cpu:		CPU handling the HPET interrupt
  * @pkgs_per_group:		Number of physical packages in a group of CPUs
  *				receiving an IPI
@@ -133,6 +134,7 @@ struct hpet_hld_data {
 	u64		tsc_next;
 	u64		tsc_ticks_per_group;
 	int		irq;
+	bool		intr_remap_enabled;
 	u32		handling_cpu;
 	u32		pkgs_per_group;
 	u32		nr_groups;
arch/x86/kernel/hpet.c +3 -0
index 3e43e0f348b8..ff4abdef5e15 100644
@@ -1464,6 +1464,9 @@ struct hpet_hld_data *hpet_hld_get_timer(void)
 	if (!hpet_domain)
 		goto err;
 
+	if (hpet_domain->parent != x86_vector_domain)
+		hld_data->intr_remap_enabled = true;
+
 	hc->mode = HPET_MODE_NMI_WATCHDOG;
 	irq = hpet_assign_irq(hpet_domain, hc, hc->num);
 	if (irq <= 0)
arch/x86/kernel/watchdog_hld_hpet.c +42 -6

81 lines not on this page · raw message

Signature · 6 lines
2.17.1

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu