Skip to contentSkip to conversation

genirq: irq_pipeline: fix irq flow handling with stacked irqchips

PATCH Superseded Philippe Gerum 2 participants 7 messages Sep 15, 2023 – Sep 18, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 genirq: irq_pipeline: fix irq flow handling with stacked irqchips

This implementation extends Hongzhan's original proposal, extending the irq flow logic in order to account for any type of stacked flow handler (although the ones masking the incoming irq were safe already).

Tested here on odroid c4 (armv8), xu4q and sabre (armv7), embedded x86 and kvm/x86, so far so good. However, I never stumbled upon the cascaded irqchip issue this aims at fixing, so we need much more testing.

---

When irqchips are stacked, a cascaded event decoded by a parent irq which is not delivered oob won't receive any ack/eoi, which is wrong. Failing to ack any unmasked edge event could quickly yield an interrupt storm. E.g. on x86, with a gpio irq controller stacked on top of the IO/APIC:

handle_irq_pipelined_prepare()
[on_pipeline_entry: true]
arch_handle_irq()
handle_fasteoi_irq()
handle_oob_irq()
mask_cond_eoi_irq()
handle_irq_pipelined_finish()
[on_pipeline_entry: false]
synchronize_pipeline_on_irq()
handle_fasteoi_irq() [!on_pipeline_entry]
intel_gpio_irq()
handle_edge_irq() <<< failed to ack

To solve this, we need to track the deferral status of every irq event, running the latter through the pipeline injection code when required, regardless of the current stage.

See https://lore.kernel.org/xenomai/MN2PR11MB42855A234CF8BF7D17E9E2ECF2E6A@MN2PR11MB4285.namprd11.prod.outlook.com/.

arch/x86/kernel/irq_pipeline.c +2 -1
index 831a04a2ea146..b590986d16990 100644
@@ -116,6 +116,8 @@ static void do_sysvec_inband(struct irq_desc *desc, struct pt_regs *regs)
 	 * don't want to break.
 	 */
 
+	irq_clear_deferral(desc);
+
 	switch (vector) {
 #ifdef CONFIG_SMP
 	case RESCHEDULE_VECTOR:
@@ -174,7 +176,6 @@ static void do_sysvec_inband(struct irq_desc *desc, struct pt_regs *regs)
 
 static void do_irq_inband(struct pt_regs *regs, u32 irq)
 {
-
 	struct irq_desc *desc = irq_to_desc(irq);
 
 	desc->handle_irq(desc);
include/linux/hardirq.h +0 -5
index cf633c758b1e4..ef41904b46304 100644
@@ -143,11 +143,6 @@ void irq_exit_rcu(void);
 		irq_pipeline_nmi_exit();			\
 	} while (0)
 
-static inline bool start_irq_flow(void)
-{
-	return !irqs_pipelined() || in_pipeline();
-}
-
 static inline bool on_pipeline_entry(void)
 {
 	return irqs_pipelined() && in_pipeline();
include/linux/irq.h +5 -0
index d242ab0df11f0..cf7139c9059b5 100644
@@ -1239,11 +1239,16 @@ static inline struct irq_chip_type *irq_data_get_chip_type(struct irq_data *d)
 
 #ifdef CONFIG_IRQ_PIPELINE
 int irq_switch_oob(unsigned int irq, bool on);
+void irq_clear_deferral(struct irq_desc *desc);
 #else
 static inline int irq_switch_oob(unsigned int irq, bool on)
 {
 	return 0;
 }
+
+static inline void irq_clear_deferral(struct irq_desc *desc)
+{
+}
 #endif	/* !CONFIG_IRQ_PIPELINE */
 
 #ifdef CONFIG_SMP
kernel/irq/chip.c +112 -16

247 lines not on this page · raw message

kernel/irq/internals.h +3 -1
index e4f66a7a24c2c..a3b40ce2e1dd7 100644
@@ -56,7 +56,8 @@ enum {
  * IRQS_SUSPENDED		- irq is suspended
  * IRQS_NMI			- irq line is used to deliver NMIs
  * IRQS_SYSFS			- descriptor has been added to sysfs
- * IRQS_EDGE			- irq line received an edge event
+ * IRQS_EDGE			- irq line received an edge event (irq_pipeline)
+ * IRQS_DEFERRED		- irq event deferred to inband (irq_pipeline)
  */
 enum {
 	IRQS_AUTODETECT		= 0x00000001,
@@ -71,6 +72,7 @@ enum {
 	IRQS_NMI		= 0x00002000,
 	IRQS_SYSFS		= 0x00004000,
 	IRQS_EDGE		= 0x00008000,
+	IRQS_DEFERRED		= 0x00010000,
 };
 
 #include "debug.h"
kernel/irq/pipeline.c +11 -2
index 550bacf2cb77d..d550a37a895e9 100644
@@ -144,7 +144,12 @@ static struct irq_domain_ops sirq_domain_ops = {
  *	@desc:	the interrupt description structure for this irq
  *
  *	Handles synthetic interrupts flowing down the IRQ pipeline
- *	with per-CPU semantics.
+ *	with per-CPU semantics (accessing desc needs no locking).
+ *
+ *	Since the SIPIC cannot be stacked on top of a parent irqchip,
+ * 	we can bypass most of the IRQS_DEFERRED logic, testing
+ * 	on_pipeline_entry() directly - although we should still clear
+ * 	IRQS_DEFERRED when in-band since handle_oob_irq() raises it.
  *
  *      CAUTION: synthetic IRQs may be used to map hardware-generated
  *      events (e.g. IPIs or traps), we must start handling them as
@@ -162,6 +167,8 @@ void handle_synthetic_irq(struct irq_desc *desc)
 		return;
 	}
 
+	irq_clear_deferral(desc);
+
 	action = desc->action;
 	if (action == NULL) {
 		if (printk_ratelimit())
@@ -955,7 +962,8 @@ static inline bool is_active_edge_event(struct irq_desc *desc)
 		!irqd_irq_disabled(&desc->irq_data);
 }
 
-bool handle_oob_irq(struct irq_desc *desc) /* hardirqs off */
+/* desc->lock held unless per-CPU, hardirqs off. */
+bool handle_oob_irq(struct irq_desc *desc)
 {
 	struct irq_stage_data *oobd = this_oob_staged();
 	unsigned int irq = irq_desc_get_irq(desc);
@@ -981,6 +989,7 @@ bool handle_oob_irq(struct irq_desc *desc) /* hardirqs off */
 	 * whether an out-of-band interrupt was delivered.
 	 */
 	if (!oob_stage_present() || !irq_settings_is_oob(desc)) {
+		desc->istate |= IRQS_DEFERRED;
 		irq_post_stage(&inband_stage, irq);
 		return false;
 	}
Signature · 1 line
Philippe.
#1 lore raw
Quoted 307 lines

-----Original Message-----
From: Philippe Gerum <[email protected]>
Sent: Friday, September 15, 2023 10:00 PM
To: [email protected]
Cc: Chen, Hongzhan <[email protected]>
Subject: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

This implementation extends Hongzhan's original proposal, extending the irq flow logic in order to account for any type of stacked flow handler (although the ones masking the incoming irq were safe already).

Tested here on odroid c4 (armv8), xu4q and sabre (armv7), embedded x86 and kvm/x86, so far so good. However, I never stumbled upon the cascaded irqchip issue this aims at fixing, so we need much more testing.

---

When irqchips are stacked, a cascaded event decoded by a parent irq which is not delivered oob won't receive any ack/eoi, which is wrong. Failing to ack any unmasked edge event could quickly yield an interrupt storm. E.g. on x86, with a gpio irq controller stacked on top of the IO/APIC:

handle_irq_pipelined_prepare()
[on_pipeline_entry: true]
arch_handle_irq()

           handle_fasteoi_irq()
                   handle_oob_irq()
                           mask_cond_eoi_irq()

handle_irq_pipelined_finish()
[on_pipeline_entry: false]
synchronize_pipeline_on_irq()
handle_fasteoi_irq() [!on_pipeline_entry]
intel_gpio_irq()
handle_edge_irq() <<< failed to ack

To solve this, we need to track the deferral status of every irq event, running the latter through the pipeline injection code when required, regardless of the current stage.

See
https://lore.kernel.org/xenomai/MN2PR11MB42855A234CF8BF7D17E9E2ECF2
[email protected]/.

arch/x86/kernel/irq_pipeline.c +2 -0
index 831a04a2ea146..b590986d16990 100644
@@ -116,6 +116,8 @@ static void do_sysvec_inband(struct irq_desc *desc,
 struct pt_regs *regs)
 	 * don't want to break.
 	 */
 
+	irq_clear_deferral(desc);
+
 	switch (vector) {
 #ifdef CONFIG_SMP

case RESCHEDULE_VECTOR:

arch/x86/kernel/irq_pipeline.c +0 -1
@@ -174,7 +176,6 @@ static void do_sysvec_inband(struct irq_desc *desc,
 struct pt_regs *regs)
 
 static void do_irq_inband(struct pt_regs *regs, u32 irq)
 {
-
 	struct irq_desc *desc = irq_to_desc(irq);
 
	desc->handle_irq(desc);
include/linux/hardirq.h +0 -5
index cf633c758b1e4..ef41904b46304 100644
@@ -143,11 +143,6 @@ void irq_exit_rcu(void);
 		irq_pipeline_nmi_exit();			\
 	} while (0)
 
-static inline bool start_irq_flow(void)
-{
-	return !irqs_pipelined() || in_pipeline();
-}
-
 static inline bool on_pipeline_entry(void)
 {
 	return irqs_pipelined() && in_pipeline();
include/linux/irq.h +5 -0
index d242ab0df11f0..cf7139c9059b5 100644
@@ -1239,11 +1239,16 @@ static inline struct irq_chip_type
 *irq_data_get_chip_type(struct irq_data *d)
 
 #ifdef CONFIG_IRQ_PIPELINE
 int irq_switch_oob(unsigned int irq, bool on);
+void irq_clear_deferral(struct irq_desc *desc);
 #else
 static inline int irq_switch_oob(unsigned int irq, bool on)
 {
 	return 0;
 }
+
+static inline void irq_clear_deferral(struct irq_desc *desc)
+{
+}
 #endif	/* !CONFIG_IRQ_PIPELINE */
 

#ifdef CONFIG_SMP

kernel/irq/chip.c +5 -0
index 05fb1f8d4713b..d17eda14784ca 100644
@@ -413,6 +413,11 @@ void irq_percpu_disable(struct irq_desc *desc,
 unsigned int cpu)
 	cpumask_clear_cpu(cpu, desc->percpu_enabled);
 }
 
+void irq_clear_deferral(struct irq_desc *desc)
+{
+	desc->istate &= ~IRQS_DEFERRED;
+}
+
 static inline void mask_ack_irq(struct irq_desc *desc)
 {
	if (desc->irq_data.chip->irq_mask_ack) {
kernel/irq/chip.c +72 -0
@@ -509,6 +514,78 @@ static bool irq_check_poll(struct irq_desc *desc)
 	return irq_wait_for_poll(desc);
 }
 
+enum {
+	IRQ_FLOW_START,
+	IRQ_FLOW_PILEUP,
+	IRQ_FLOW_REPLAY,
+};
+
+/*
+ * irq_starting_flow - Determine whether we are actually starting an
+ * interrupt flow.
+ *
+ * When interrupt pipelining is enabled, interrupt flow handlers may
+ * be called twice for each event, once from the pipeline entry
+ * context in an attempt to handle the hardware event from the oob
+ * stage, a second time if the event was deferred to the in-band
+ * stage. In the first case, we have to ack the hardware before
+ * feeding the pipeline. Otherwise, we should simply run the action
+ * associated to the event from the in-band stage.
+ *
+ * With stacked irqchips, handlers of parent irqs not delivered to the
+ * oob stage would decode and post the cascaded events to the in-band
+ * stage as well. The internal per-descriptor IRQS_DEFERRED flag
+ * disambiguates between the latter context and events being replayed
+ * after deferral to the in-band stage, so that we do ack the hardware
+ * for cascaded events as expected.
+ *
+ * We might also receive a subsequent unmasked edge event on the same
+ * line while a deferred one is still pending replay from the in-band
+ * stage (hard irqs are on for replay). The flow state enumeration
+ * disambiguates all cases as follows:
+ *
+ * - IRQ_FLOW_START: we are running oob on behalf of the low-level irq
+ *   entry code, no deferred event is pending replay for the same
+ *   line, we need to send ack to the hardware.
+ *
+ * - IRQ_FLOW_PILEUP: we are running in-band for synchronizing the irq
+ *   log, one or more deferred events are already pending replay for
+ *   the same line, we need to ack the subsequent event to the
+ *   hardware too.
+ *
+ * - IRQ_FLOW_REPLAY: we are (re)playing a deferred event from the
+ *   in-band stage, as a result of synchronizing the irq log.
+ */
+static inline unsigned int irq_starting_flow(struct irq_desc *desc)
+{
+	if (!irqs_pipelined())
+		return IRQ_FLOW_START;
+
+	if (!in_pipeline())
+		return IRQ_FLOW_REPLAY;
+
+	return desc->istate & IRQS_DEFERRED ? IRQ_FLOW_PILEUP :
 IRQ_FLOW_START;
+}
+
+/*
+ * irq_feeding_pipeline - Determine whether we should be feeding the
+ * interrupt pipeline with the incoming event.
+ *
+ * Depending on the flow state determined by irq_starting_flow(), the
+ * event is passed to the oob handler or directly replayed from the
+ * current in-band stage.
+ */
+static inline bool irq_feeding_pipeline(struct irq_desc *desc, int state)
+{
+	if (irqs_pipelined()) {
+		if (state != IRQ_FLOW_REPLAY)
+			return true;
+		irq_clear_deferral(desc);
+	}
+
+	return false;
+}
+
 static bool irq_may_run(struct irq_desc *desc)
 {
	unsigned int mask = IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED;
kernel/irq/chip.c +22 -9
@@ -561,12 +638,15 @@ static bool irq_may_run(struct irq_desc *desc)
 */
 void handle_simple_irq(struct irq_desc *desc)
 {
+	int flow;
+
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow() && !irq_may_run(desc))
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc))
 		goto out_unlock;
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		handle_oob_irq(desc);
 		goto out_unlock;
 	}
@@ -601,12 +681,15 @@ EXPORT_SYMBOL_GPL(handle_simple_irq);
 */
 void handle_untracked_irq(struct irq_desc *desc)
 {
+	int flow;
+
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow() && !irq_may_run(desc))
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc))
 		goto out_unlock;
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		handle_oob_irq(desc);
 		goto out_unlock;
 	}
@@ -675,16 +758,19 @@ static void cond_unmask_irq(struct irq_desc *desc)
 */
 void handle_level_irq(struct irq_desc *desc)
 {
+	int flow;
+
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		mask_ack_irq(desc);
 
 		if (!irq_may_run(desc))
 			goto out_unlock;
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		if (handle_oob_irq(desc))
 			goto out_unmask;
 		goto out_unlock;
@@ -749,6 +835,7 @@ static void cond_unmask_eoi_irq(struct irq_desc
 *desc, struct irq_chip *chip)
 void handle_fasteoi_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = desc->irq_data.chip;
+	int flow;
 
 	raw_spin_lock(&desc->lock);
@@ -757,13 +844,14 @@ void handle_fasteoi_irq(struct irq_desc *desc)
 	 * can arrive on the new CPU before the original CPU has completed
 	 * handling the previous one - it may need to be resent.
 	 */
-	if (start_irq_flow() && !irq_may_run(desc)) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc)) {
 		if (irqd_needs_resend_when_in_progress(&desc->irq_data))
 			desc->istate |= IRQS_PENDING;
 		goto out;
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		if (handle_oob_irq(desc))
 			chip->irq_eoi(&desc->irq_data);
 		else
@@ -858,10 +946,12 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
 void handle_edge_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {

kernel/irq/chip.c void handle_edge_irq(struct irq_desc *desc) · line 977

Quoted 8 lines
kernel/irq/chip.c +1 -1
@@ -881,7 +971,7 @@ void handle_edge_irq(struct irq_desc *desc)
 		}
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		desc->istate |= IRQS_EDGE;
 		handle_oob_irq(desc);

When the irq is PILEUP, actually the previous IRQS_DEFERRED bit has not been cleared and the logged in-band irq has not been serviced yet. Do we need to call handle_oob_irq to set IRQS_DEFERRED and log the inband irq again when we already find the irq is PILEUP?

Regards

Hongzhan Chen

Quoted 135 lines
kernel/irq/chip.c +12 -6
@@ -934,10 +1024,12 @@ EXPORT_SYMBOL(handle_edge_irq);
 void handle_edge_eoi_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -946,7 +1038,7 @@ void handle_edge_eoi_irq(struct irq_desc *desc)
 		}
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		desc->istate |= IRQS_EDGE;
 		if (handle_oob_irq(desc))
 			goto out_eoi;
@@ -1341,13 +1433,15 @@ void irq_cpu_offline(void)
 void handle_fasteoi_ack_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = desc->irq_data.chip;
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow() && !irq_may_run(desc))
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc))
 		goto out;
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		if (handle_oob_irq(desc))
 			chip->irq_eoi(&desc->irq_data);
@@ -1404,17 +1498,19 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_ack_irq);
 void handle_fasteoi_mask_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = desc->irq_data.chip;
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		mask_ack_irq(desc);
 
 		if (!irq_may_run(desc))
 			goto out;
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		if (handle_oob_irq(desc))
 			chip->irq_eoi(&desc->irq_data);
 		else
kernel/irq/internals.h +3 -1
index e4f66a7a24c2c..a3b40ce2e1dd7 100644
@@ -56,7 +56,8 @@ enum {
 * IRQS_SUSPENDED		- irq is suspended
 * IRQS_NMI			- irq line is used to deliver NMIs
 * IRQS_SYSFS			- descriptor has been added to sysfs
- * IRQS_EDGE			- irq line received an edge event
+ * IRQS_EDGE			- irq line received an edge event (irq_pipeline)
+ * IRQS_DEFERRED		- irq event deferred to inband (irq_pipeline)
 */
 enum {
 	IRQS_AUTODETECT		= 0x00000001,
@@ -71,6 +72,7 @@ enum {
 	IRQS_NMI		= 0x00002000,
 	IRQS_SYSFS		= 0x00004000,
 	IRQS_EDGE		= 0x00008000,
+	IRQS_DEFERRED		= 0x00010000,
 };
 
 #include "debug.h"
kernel/irq/pipeline.c +10 -2
index 550bacf2cb77d..d550a37a895e9 100644
@@ -144,7 +144,12 @@ static struct irq_domain_ops sirq_domain_ops = {
 *	@desc:	the interrupt description structure for this irq
 *
 *	Handles synthetic interrupts flowing down the IRQ pipeline
- *	with per-CPU semantics.
+ *	with per-CPU semantics (accessing desc needs no locking).
+ *
+ *	Since the SIPIC cannot be stacked on top of a parent irqchip,
+ * 	we can bypass most of the IRQS_DEFERRED logic, testing
+ * 	on_pipeline_entry() directly - although we should still clear
+ * 	IRQS_DEFERRED when in-band since handle_oob_irq() raises it.
 *
 *      CAUTION: synthetic IRQs may be used to map hardware-generated
 *      events (e.g. IPIs or traps), we must start handling them as
@@ -162,6 +167,8 @@ void handle_synthetic_irq(struct irq_desc *desc)
 		return;
 	}
 
+	irq_clear_deferral(desc);
+
 	action = desc->action;
 	if (action == NULL) {
 		if (printk_ratelimit())
@@ -955,7 +962,8 @@ static inline bool is_active_edge_event(struct irq_desc
 *desc)
 		!irqd_irq_disabled(&desc->irq_data);
 }
 
-bool handle_oob_irq(struct irq_desc *desc) /* hardirqs off */
+/* desc->lock held unless per-CPU, hardirqs off. */
+bool handle_oob_irq(struct irq_desc *desc)
 {
 	struct irq_stage_data *oobd = this_oob_staged();
	unsigned int irq = irq_desc_get_irq(desc);
kernel/irq/pipeline.c +0 -0
@@ -981,6 +989,7 @@ bool handle_oob_irq(struct irq_desc *desc) /*

hardirqs off */

	 * whether an out-of-band interrupt was delivered.
	 */
	if (!oob_stage_present() || !irq_settings_is_oob(desc)) {
kernel/irq/pipeline.c +1 -0
+		desc->istate |= IRQS_DEFERRED;
		irq_post_stage(&inband_stage, irq);
		return false;
	}

--
Philippe.

#2 lore raw
Quoted 320 lines

-----Original Message-----
From: Chen, Hongzhan <[email protected]>
Sent: Sunday, September 17, 2023 3:16 PM
To: Philippe Gerum <[email protected]>; [email protected]
Subject: RE: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

Quoted 38 lines

-----Original Message-----
From: Philippe Gerum <[email protected]>
Sent: Friday, September 15, 2023 10:00 PM
To: [email protected]
Cc: Chen, Hongzhan <[email protected]>
Subject: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

This implementation extends Hongzhan's original proposal, extending the irq flow logic in order to account for any type of stacked flow handler (although the ones masking the incoming irq were safe already).

Tested here on odroid c4 (armv8), xu4q and sabre (armv7), embedded x86 and kvm/x86, so far so good. However, I never stumbled upon the cascaded irqchip issue this aims at fixing, so we need much more testing.

---

When irqchips are stacked, a cascaded event decoded by a parent irq which is not delivered oob won't receive any ack/eoi, which is wrong. Failing to ack any unmasked edge event could quickly yield an interrupt storm. E.g. on x86, with a gpio irq controller stacked on top of the IO/APIC:

handle_irq_pipelined_prepare()
[on_pipeline_entry: true]
arch_handle_irq()

           handle_fasteoi_irq()
                   handle_oob_irq()
                           mask_cond_eoi_irq()

handle_irq_pipelined_finish()
[on_pipeline_entry: false]
synchronize_pipeline_on_irq()
handle_fasteoi_irq() [!on_pipeline_entry]
intel_gpio_irq()
handle_edge_irq() <<< failed to ack

Quoted 6 lines

To solve this, we need to track the deferral status of every irq event, running the latter through the pipeline injection code when required, regardless of the current stage.

See
https://lore.kernel.org/xenomai/MN2PR11MB42855A234CF8BF7D17E9E2ECF

2

Quoted 264 lines

[email protected]/.

arch/x86/kernel/irq_pipeline.c +2 -0
index 831a04a2ea146..b590986d16990 100644
@@ -116,6 +116,8 @@ static void do_sysvec_inband(struct irq_desc *desc,
 struct pt_regs *regs)
 	 * don't want to break.
 	 */
 
+	irq_clear_deferral(desc);
+
 	switch (vector) {
 #ifdef CONFIG_SMP

case RESCHEDULE_VECTOR:

arch/x86/kernel/irq_pipeline.c +0 -1
@@ -174,7 +176,6 @@ static void do_sysvec_inband(struct irq_desc *desc,
 struct pt_regs *regs)
 
 static void do_irq_inband(struct pt_regs *regs, u32 irq)
 {
-
 	struct irq_desc *desc = irq_to_desc(irq);
 
	desc->handle_irq(desc);
include/linux/hardirq.h +0 -5
index cf633c758b1e4..ef41904b46304 100644
@@ -143,11 +143,6 @@ void irq_exit_rcu(void);
 		irq_pipeline_nmi_exit();			\
 	} while (0)
 
-static inline bool start_irq_flow(void)
-{
-	return !irqs_pipelined() || in_pipeline();
-}
-
 static inline bool on_pipeline_entry(void)
 {
 	return irqs_pipelined() && in_pipeline();
include/linux/irq.h +5 -0
index d242ab0df11f0..cf7139c9059b5 100644
@@ -1239,11 +1239,16 @@ static inline struct irq_chip_type
 *irq_data_get_chip_type(struct irq_data *d)
 
 #ifdef CONFIG_IRQ_PIPELINE
 int irq_switch_oob(unsigned int irq, bool on);
+void irq_clear_deferral(struct irq_desc *desc);
 #else
 static inline int irq_switch_oob(unsigned int irq, bool on)
 {
 	return 0;
 }
+
+static inline void irq_clear_deferral(struct irq_desc *desc)
+{
+}
 #endif	/* !CONFIG_IRQ_PIPELINE */
 

#ifdef CONFIG_SMP

kernel/irq/chip.c +5 -0
index 05fb1f8d4713b..d17eda14784ca 100644
@@ -413,6 +413,11 @@ void irq_percpu_disable(struct irq_desc *desc,
 unsigned int cpu)
 	cpumask_clear_cpu(cpu, desc->percpu_enabled);
 }
 
+void irq_clear_deferral(struct irq_desc *desc)
+{
+	desc->istate &= ~IRQS_DEFERRED;
+}
+
 static inline void mask_ack_irq(struct irq_desc *desc)
 {
	if (desc->irq_data.chip->irq_mask_ack) {
kernel/irq/chip.c +72 -0
@@ -509,6 +514,78 @@ static bool irq_check_poll(struct irq_desc *desc)
 	return irq_wait_for_poll(desc);
 }
 
+enum {
+	IRQ_FLOW_START,
+	IRQ_FLOW_PILEUP,
+	IRQ_FLOW_REPLAY,
+};
+
+/*
+ * irq_starting_flow - Determine whether we are actually starting an
+ * interrupt flow.
+ *
+ * When interrupt pipelining is enabled, interrupt flow handlers may
+ * be called twice for each event, once from the pipeline entry
+ * context in an attempt to handle the hardware event from the oob
+ * stage, a second time if the event was deferred to the in-band
+ * stage. In the first case, we have to ack the hardware before
+ * feeding the pipeline. Otherwise, we should simply run the action
+ * associated to the event from the in-band stage.
+ *
+ * With stacked irqchips, handlers of parent irqs not delivered to the
+ * oob stage would decode and post the cascaded events to the in-band
+ * stage as well. The internal per-descriptor IRQS_DEFERRED flag
+ * disambiguates between the latter context and events being replayed
+ * after deferral to the in-band stage, so that we do ack the hardware
+ * for cascaded events as expected.
+ *
+ * We might also receive a subsequent unmasked edge event on the same
+ * line while a deferred one is still pending replay from the in-band
+ * stage (hard irqs are on for replay). The flow state enumeration
+ * disambiguates all cases as follows:
+ *
+ * - IRQ_FLOW_START: we are running oob on behalf of the low-level irq
+ *   entry code, no deferred event is pending replay for the same
+ *   line, we need to send ack to the hardware.
+ *
+ * - IRQ_FLOW_PILEUP: we are running in-band for synchronizing the irq
+ *   log, one or more deferred events are already pending replay for
+ *   the same line, we need to ack the subsequent event to the
+ *   hardware too.
+ *
+ * - IRQ_FLOW_REPLAY: we are (re)playing a deferred event from the
+ *   in-band stage, as a result of synchronizing the irq log.
+ */
+static inline unsigned int irq_starting_flow(struct irq_desc *desc)
+{
+	if (!irqs_pipelined())
+		return IRQ_FLOW_START;
+
+	if (!in_pipeline())
+		return IRQ_FLOW_REPLAY;
+
+	return desc->istate & IRQS_DEFERRED ? IRQ_FLOW_PILEUP :
 IRQ_FLOW_START;
+}
+
+/*
+ * irq_feeding_pipeline - Determine whether we should be feeding the
+ * interrupt pipeline with the incoming event.
+ *
+ * Depending on the flow state determined by irq_starting_flow(), the
+ * event is passed to the oob handler or directly replayed from the
+ * current in-band stage.
+ */
+static inline bool irq_feeding_pipeline(struct irq_desc *desc, int state)
+{
+	if (irqs_pipelined()) {
+		if (state != IRQ_FLOW_REPLAY)
+			return true;
+		irq_clear_deferral(desc);
+	}
+
+	return false;
+}
+
 static bool irq_may_run(struct irq_desc *desc)
 {
	unsigned int mask = IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED;
kernel/irq/chip.c +22 -9
@@ -561,12 +638,15 @@ static bool irq_may_run(struct irq_desc *desc)
 */
 void handle_simple_irq(struct irq_desc *desc)
 {
+	int flow;
+
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow() && !irq_may_run(desc))
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc))
 		goto out_unlock;
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		handle_oob_irq(desc);
 		goto out_unlock;
 	}
@@ -601,12 +681,15 @@ EXPORT_SYMBOL_GPL(handle_simple_irq);
 */
 void handle_untracked_irq(struct irq_desc *desc)
 {
+	int flow;
+
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow() && !irq_may_run(desc))
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc))
 		goto out_unlock;
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		handle_oob_irq(desc);
 		goto out_unlock;
 	}
@@ -675,16 +758,19 @@ static void cond_unmask_irq(struct irq_desc *desc)
 */
 void handle_level_irq(struct irq_desc *desc)
 {
+	int flow;
+
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		mask_ack_irq(desc);
 
 		if (!irq_may_run(desc))
 			goto out_unlock;
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		if (handle_oob_irq(desc))
 			goto out_unmask;
 		goto out_unlock;
@@ -749,6 +835,7 @@ static void cond_unmask_eoi_irq(struct irq_desc
 *desc, struct irq_chip *chip)
 void handle_fasteoi_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = desc->irq_data.chip;
+	int flow;
 
 	raw_spin_lock(&desc->lock);
@@ -757,13 +844,14 @@ void handle_fasteoi_irq(struct irq_desc *desc)
 	 * can arrive on the new CPU before the original CPU has completed
 	 * handling the previous one - it may need to be resent.
 	 */
-	if (start_irq_flow() && !irq_may_run(desc)) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc)) {
 		if (irqd_needs_resend_when_in_progress(&desc->irq_data))
 			desc->istate |= IRQS_PENDING;
 		goto out;
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		if (handle_oob_irq(desc))
 			chip->irq_eoi(&desc->irq_data);
 		else
@@ -858,10 +946,12 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
 void handle_edge_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -881,7 +971,7 @@ void handle_edge_irq(struct irq_desc *desc)
 		}
 	}
 
Quoted 10 lines

kernel/irq/chip.c void handle_edge_irq(struct irq_desc *desc) · line 977

Quoted 5 lines
kernel/irq/chip.c +1 -1
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		desc->istate |= IRQS_EDGE;
 		handle_oob_irq(desc);

When the irq is PILEUP, actually the previous IRQS_DEFERRED bit has not been cleared and the logged in-band irq has not been serviced yet. Do we need to call handle_oob_irq to set IRQS_DEFERRED and log the inband irq again when

It is meaningful to log the inband irq via calling irq_post_stage from handle_oob_irq
in case current irq actually preempted in do_inband_irq where tried to service previous same irq number and already cleared the log before calling do_inband_irq.

Regards

Hongzhan Chen

Quoted 143 lines

we already find the irq is PILEUP?

Regards

Hongzhan Chen

Quoted 68 lines
kernel/irq/chip.c +12 -6
@@ -934,10 +1024,12 @@ EXPORT_SYMBOL(handle_edge_irq);
 void handle_edge_eoi_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -946,7 +1038,7 @@ void handle_edge_eoi_irq(struct irq_desc *desc)
 		}
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		desc->istate |= IRQS_EDGE;
 		if (handle_oob_irq(desc))
 			goto out_eoi;
@@ -1341,13 +1433,15 @@ void irq_cpu_offline(void)
 void handle_fasteoi_ack_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = desc->irq_data.chip;
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow() && !irq_may_run(desc))
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY && !irq_may_run(desc))
 		goto out;
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		if (handle_oob_irq(desc))
 			chip->irq_eoi(&desc->irq_data);
@@ -1404,17 +1498,19 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_ack_irq);
 void handle_fasteoi_mask_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = desc->irq_data.chip;
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		mask_ack_irq(desc);
 
 		if (!irq_may_run(desc))
 			goto out;
 	}
 
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		if (handle_oob_irq(desc))
 			chip->irq_eoi(&desc->irq_data);
 		else
kernel/irq/internals.h +0 -0
index e4f66a7a24c2c..a3b40ce2e1dd7 100644

kernel/irq/internals.h enum { · line 59

Quoted 5 lines
kernel/irq/internals.h +1 -1
@@ -56,7 +56,8 @@ enum {
 * IRQS_SUSPENDED		- irq is suspended
 * IRQS_NMI			- irq line is used to deliver NMIs
 * IRQS_SYSFS			- descriptor has been added to sysfs
- * IRQS_EDGE			- irq line received an edge event
+ * IRQS_EDGE			- irq line received an edge event

(irq_pipeline)

kernel/irq/pipeline.c static inline bool is_active_edge_event(struct

Quoted 40 lines
kernel/irq/internals.h +2 -0
+ * IRQS_DEFERRED		- irq event deferred to inband (irq_pipeline)
 */
 enum {
 	IRQS_AUTODETECT		= 0x00000001,
@@ -71,6 +72,7 @@ enum {
 	IRQS_NMI		= 0x00002000,
 	IRQS_SYSFS		= 0x00004000,
 	IRQS_EDGE		= 0x00008000,
+	IRQS_DEFERRED		= 0x00010000,
 };
 
 #include "debug.h"
kernel/irq/pipeline.c +8 -1
index 550bacf2cb77d..d550a37a895e9 100644
@@ -144,7 +144,12 @@ static struct irq_domain_ops sirq_domain_ops = {
 *	@desc:	the interrupt description structure for this irq
 *
 *	Handles synthetic interrupts flowing down the IRQ pipeline
- *	with per-CPU semantics.
+ *	with per-CPU semantics (accessing desc needs no locking).
+ *
+ *	Since the SIPIC cannot be stacked on top of a parent irqchip,
+ * 	we can bypass most of the IRQS_DEFERRED logic, testing
+ * 	on_pipeline_entry() directly - although we should still clear
+ * 	IRQS_DEFERRED when in-band since handle_oob_irq() raises it.
 *
 *      CAUTION: synthetic IRQs may be used to map hardware-generated
 *      events (e.g. IPIs or traps), we must start handling them as
@@ -162,6 +167,8 @@ void handle_synthetic_irq(struct irq_desc *desc)
 		return;
 	}
 
+	irq_clear_deferral(desc);
+
 	action = desc->action;
 	if (action == NULL) {
 		if (printk_ratelimit())
@@ -955,7 +962,8 @@ static inline bool is_active_edge_event(struct

irq_desc

Quoted 22 lines
kernel/irq/pipeline.c +2 -1
 *desc)
 		!irqd_irq_disabled(&desc->irq_data);
 }
 
-bool handle_oob_irq(struct irq_desc *desc) /* hardirqs off */
+/* desc->lock held unless per-CPU, hardirqs off. */
+bool handle_oob_irq(struct irq_desc *desc)
 {
 	struct irq_stage_data *oobd = this_oob_staged();
	unsigned int irq = irq_desc_get_irq(desc);
kernel/irq/pipeline.c +0 -0
@@ -981,6 +989,7 @@ bool handle_oob_irq(struct irq_desc *desc) /*

hardirqs off */

	 * whether an out-of-band interrupt was delivered.
	 */
	if (!oob_stage_present() || !irq_settings_is_oob(desc)) {
kernel/irq/pipeline.c +1 -0
+		desc->istate |= IRQS_DEFERRED;
		irq_post_stage(&inband_stage, irq);
		return false;
	}

--
Philippe.

#3 lore raw

"Chen, Hongzhan" <[email protected]> writes:

[snip]

Quoted 29 lines
Quoted 18 lines
Quoted 18 lines
kernel/irq/chip.c +3 -1
@@ -858,10 +946,12 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
 void handle_edge_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -881,7 +971,7 @@ void handle_edge_irq(struct irq_desc *desc)
 		}
 	}
 
Quoted 10 lines

kernel/irq/chip.c void handle_edge_irq(struct irq_desc *desc) · line 977

Quoted 5 lines
kernel/irq/chip.c +1 -1
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		desc->istate |= IRQS_EDGE;
 		handle_oob_irq(desc);

When the irq is PILEUP, actually the previous IRQS_DEFERRED bit has not been cleared and the logged in-band irq has not been serviced yet. Do we need to call handle_oob_irq to set IRQS_DEFERRED and log the inband irq again when

Quoted 3 lines

It is meaningful to log the inband irq via calling irq_post_stage from handle_oob_irq
in case current irq actually preempted in do_inband_irq where tried to service previous same irq number and already cleared the log before calling do_inband_irq.

That's correct, preemption might only happen after the initial irq event was cleared in the log, but also after IRQS_DEFERRED was cleared in the flow handler while replaying this event, so we need to set the deferral flag again for the subsequent event. IOW, the code path from the call to irq_feeding_pipeline() back to disabling hard irqs on return to do_inband_irq() is not atomic. Fortunately, sync_current_irq_stage() loops as long as events are pending in the log, so we won't miss the piling ones.

Is this patch fixing the original issue you detected on your fixture?

Signature · 1 line
Philippe.
#4 lore raw
Quoted 63 lines

-----Original Message-----
From: Philippe Gerum <[email protected]>
Sent: Sunday, September 17, 2023 4:54 PM
To: Chen, Hongzhan <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

"Chen, Hongzhan" <[email protected]> writes:

[snip]

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
kernel/irq/chip.c +3 -1
@@ -858,10 +946,12 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
 void handle_edge_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -881,7 +971,7 @@ void handle_edge_irq(struct irq_desc *desc)
 		}
 	}
 
Quoted 8 lines
Quoted 8 lines

kernel/irq/chip.c void handle_edge_irq(struct irq_desc *desc) · line 977

Quoted 5 lines
kernel/irq/chip.c +1 -1
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		desc->istate |= IRQS_EDGE;
 		handle_oob_irq(desc);

When the irq is PILEUP, actually the previous IRQS_DEFERRED bit has not

been

Quoted 1 line
Quoted 1 line

cleared and the logged in-band irq has not been serviced yet. Do we need

to

Quoted 1 line
Quoted 1 line

call handle_oob_irq to set IRQS_DEFERRED and log the inband irq again

when

Quoted 2 lines

It is meaningful to log the inband irq via calling irq_post_stage from

handle_oob_irq

Quoted 1 line

in case current irq actually preempted in do_inband_irq where tried to

service previous same irq number and already cleared the log before calling do_inband_irq.

Quoted 1 line

That's correct, preemption might only happen after the initial irq event was cleared in the log, but also after IRQS_DEFERRED was cleared in the flow handler while replaying this event, so we need to set the deferral flag again for the subsequent event. IOW, the code path from the call to irq_feeding_pipeline() back to disabling hard irqs on return to do_inband_irq() is not atomic. Fortunately, sync_current_irq_stage() loops as long as events are pending in the log, so we won't miss the piling ones.

Quoted 1 line

Is this patch fixing the original issue you detected on your fixture?

Logically, it fix the the issue but I have not tested it yet. I will validate it tomorrow when I am on duty and then give you feedback.

Regards

Hongzhan Chen

Quoted 3 lines

--
Philippe.

#5 lore raw
Quoted 73 lines

-----Original Message-----
From: Chen, Hongzhan <[email protected]>
Sent: Sunday, September 17, 2023 9:43 PM
To: Philippe Gerum <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

Quoted 61 lines

-----Original Message-----
From: Philippe Gerum <[email protected]>
Sent: Sunday, September 17, 2023 4:54 PM
To: Chen, Hongzhan <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

"Chen, Hongzhan" <[email protected]> writes:

[snip]

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
kernel/irq/chip.c +3 -1
@@ -858,10 +946,12 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
 void handle_edge_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -881,7 +971,7 @@ void handle_edge_irq(struct irq_desc *desc)
 		}
 	}
 
Quoted 8 lines
Quoted 8 lines

kernel/irq/chip.c void handle_edge_irq(struct irq_desc *desc) · line 977

Quoted 5 lines
kernel/irq/chip.c +1 -1
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		desc->istate |= IRQS_EDGE;
 		handle_oob_irq(desc);

When the irq is PILEUP, actually the previous IRQS_DEFERRED bit has not

been

Quoted 1 line
Quoted 1 line

cleared and the logged in-band irq has not been serviced yet. Do we need

to

Quoted 1 line
Quoted 1 line

call handle_oob_irq to set IRQS_DEFERRED and log the inband irq again

when

Quoted 2 lines

It is meaningful to log the inband irq via calling irq_post_stage from

handle_oob_irq

Quoted 1 line

in case current irq actually preempted in do_inband_irq where tried to

service previous same irq number and already cleared the log before calling do_inband_irq.

Quoted 1 line

That's correct, preemption might only happen after the initial irq event was cleared in the log, but also after IRQS_DEFERRED was cleared in the flow handler while replaying this event, so we need to set the deferral flag again for the subsequent event. IOW, the code path from the call to irq_feeding_pipeline() back to disabling hard irqs on return to do_inband_irq() is not atomic. Fortunately, sync_current_irq_stage() loops as long as events are pending in the log, so we won't miss the piling ones.

Quoted 5 lines
Quoted 1 line

Is this patch fixing the original issue you detected on your fixture?

Logically, it fix the the issue but I have not tested it yet. I will validate it tomorrow
when I am on duty and then give you feedback.

The issue still exists after apply the patch because irq_feeding_pipeline never return true for the cascaded edge_irq so that it would never call chip->irq_ack(&desc->irq_data) and handle_oob_irq(desc) in handle_edge_irq

handle_irq_pipelined_prepare()
[on_pipeline_entry: true]
arch_handle_irq()
handle_fasteoi_irq()
handle_oob_irq()
mask_cond_eoi_irq()
handle_irq_pipelined_finish()
[on_pipeline_entry: false]
synchronize_pipeline_on_irq()
handle_fasteoi_irq() [!on_pipeline_entry]
intel_gpio_irq()
handle_edge_irq()
irq_starting_flow <<< return IRQ_FLOW_REPLAY irq_feeding_pipeline <<< return false

Regards

Hongzhan Chen

Quoted 7 lines

Regards

Hongzhan Chen

Quoted 3 lines

--
Philippe.

#6 lore raw
Quoted 80 lines · Hongzhan
Quoted 71 lines

-----Original Message-----
From: Chen, Hongzhan <[email protected]>
Sent: Sunday, September 17, 2023 9:43 PM
To: Philippe Gerum <[email protected]>
Cc: [email protected]
Subject: RE: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

Quoted 61 lines

-----Original Message-----
From: Philippe Gerum <[email protected]>
Sent: Sunday, September 17, 2023 4:54 PM
To: Chen, Hongzhan <[email protected]>
Cc: [email protected]
Subject: Re: [PATCH] genirq: irq_pipeline: fix irq flow handling with stacked irqchips

"Chen, Hongzhan" <[email protected]> writes:

[snip]

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
kernel/irq/chip.c +3 -1
@@ -858,10 +946,12 @@ EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
 void handle_edge_irq(struct irq_desc *desc)
 {
 	struct irq_chip *chip = irq_desc_get_chip(desc);
+	int flow;
 
 	raw_spin_lock(&desc->lock);
 
-	if (start_irq_flow()) {
+	flow = irq_starting_flow(desc);
+	if (flow != IRQ_FLOW_REPLAY) {
 		desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
 
 		if (!irq_may_run(desc)) {
@@ -881,7 +971,7 @@ void handle_edge_irq(struct irq_desc *desc)
 		}
 	}
 
Quoted 8 lines
Quoted 8 lines

kernel/irq/chip.c void handle_edge_irq(struct irq_desc *desc) · line 977

Quoted 5 lines
kernel/irq/chip.c +1 -1
-	if (on_pipeline_entry()) {
+	if (irq_feeding_pipeline(desc, flow)) {
 		chip->irq_ack(&desc->irq_data);
 		desc->istate |= IRQS_EDGE;
 		handle_oob_irq(desc);

When the irq is PILEUP, actually the previous IRQS_DEFERRED bit has not

been

Quoted 1 line
Quoted 1 line

cleared and the logged in-band irq has not been serviced yet. Do we need

to

Quoted 1 line
Quoted 1 line

call handle_oob_irq to set IRQS_DEFERRED and log the inband irq again

when

Quoted 2 lines

It is meaningful to log the inband irq via calling irq_post_stage from

handle_oob_irq

Quoted 1 line

in case current irq actually preempted in do_inband_irq where tried to

service previous same irq number and already cleared the log before calling do_inband_irq.

Quoted 1 line

That's correct, preemption might only happen after the initial irq event was cleared in the log, but also after IRQS_DEFERRED was cleared in the flow handler while replaying this event, so we need to set the deferral flag again for the subsequent event. IOW, the code path from the call to irq_feeding_pipeline() back to disabling hard irqs on return to do_inband_irq() is not atomic. Fortunately, sync_current_irq_stage() loops as long as events are pending in the log, so we won't miss the piling ones.

Quoted 5 lines
Quoted 1 line

Is this patch fixing the original issue you detected on your fixture?

Logically, it fix the the issue but I have not tested it yet. I will validate it tomorrow
when I am on duty and then give you feedback.

The issue still exists after apply the patch because irq_feeding_pipeline never return true for the cascaded edge_irq so that it would never call chip->irq_ack(&desc->irq_data) and handle_oob_irq(desc) in handle_edge_irq

Quoted 15 lines

handle_irq_pipelined_prepare()
[on_pipeline_entry: true]
arch_handle_irq()
handle_fasteoi_irq()
handle_oob_irq()
mask_cond_eoi_irq()
handle_irq_pipelined_finish()
[on_pipeline_entry: false]
synchronize_pipeline_on_irq()
handle_fasteoi_irq() [!on_pipeline_entry]
intel_gpio_irq()
handle_edge_irq()
irq_starting_flow <<< return IRQ_FLOW_REPLAY irq_feeding_pipeline <<< return false

Which makes sense, the logic irq_starting_flow() is half-baked. Working on this.

Signature · 1 line
Philippe.