Skip to contentSkip to conversation

arm64: Fix cpuidle with pseudo-NMI enabled

PATCH Superseded Marc Zyngier 4 participants 18 messages Jun 8, 2021 – Jun 16, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover arm64: Fix cpuidle with pseudo-NMI enabled

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

Instead of spreading more magic code around, I've elected to provide a pair of helpers (arm_cpuidle_{save,restore}_context()) which do the heavy lifting.

With that in place, I can finally boot the above system with irqchip.gicv3_pseudo_nmi=1. I'd welcome feedback from people who may have experienced similar issues in the past (and on different machines).

Marc Zyngier (3):
arm64: Add cpuidle context save/restore helpers
arm64: Convert cpu_do_idle() to using cpuidle context helpers
PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

 arch/arm/include/asm/cpuidle.h   |  5 ++++
 arch/arm64/include/asm/cpuidle.h | 35 +++++++++++++++++++++++++++
 arch/arm64/kernel/process.c      | 41 +++++++-------------------------
 drivers/firmware/psci/psci.c     |  5 ++++
 4 files changed, 54 insertions(+), 32 deletions(-)
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw

PATCH 1/3 arm64: Add cpuidle context save/restore helpers

As we need to start doing some additional work on all idle paths, let's introduce a set of macros that will perform the work related to the GICv3 pseudo-NMI idle entry exit.

Stubs are introduced to 32bit ARM for compatibility. As these helpers are currently unused, the is no functional change.

---
 arch/arm/include/asm/cpuidle.h   |  5 +++++
 arch/arm64/include/asm/cpuidle.h | 35 ++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/arm/include/asm/cpuidle.h +5 -0
index 0d67ed682e07..1e0b8da12d96 100644
@@ -49,4 +49,9 @@ extern int arm_cpuidle_suspend(int index);
 
 extern int arm_cpuidle_init(int cpu);
 
+struct arm_cpuidle_context { };
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+
 #endif
arch/arm64/include/asm/cpuidle.h +35 -0
index 3c5ddb429ea2..53adad0a5c7e 100644
@@ -18,4 +18,39 @@ static inline int arm_cpuidle_suspend(int index)
 	return -EOPNOTSUPP;
 }
 #endif
+
+#ifdef CONFIG_ARM64_PSEUDO_NMI
+#include <asm/arch_gicv3.h>
+
+struct arm_cpuidle_context {
+	unsigned long pmr;
+	unsigned long daif_bits;
+};
+
+#define arm_cpuidle_save_context(__c)					\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			c->daif_bits = read_sysreg(daif);		\
+			write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \
+				     daif);				\
+			c->pmr = gic_read_pmr();			\
+			gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \
+		}							\
+	} while (0)
+
+#define arm_cpuidle_restore_context(__c)				\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			gic_write_pmr(c->pmr);				\
+			write_sysreg(c->daif_bits, daif);		\
+		}							\
+	} while (0)
+#else
+struct arm_cpuidle_context { };
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+#endif
 #endif
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#5 lore raw

PATCH 2/3 arm64: Convert cpu_do_idle() to using cpuidle context helpers

In reply to Marc Zyngier

Now that we have helpers that are aware of the pseudo-NMI feature, introduce them to cpu_do_idle(). This allows for some nice cleanup.

No functional change intended.

---
 arch/arm64/kernel/process.c | 41 ++++++++-----------------------------
 1 file changed, 9 insertions(+), 32 deletions(-)
arch/arm64/kernel/process.c +9 -32
index b4bb67f17a2c..f99144eda8dc 100644
@@ -46,9 +46,9 @@
 #include <linux/prctl.h>
 
 #include <asm/alternative.h>
-#include <asm/arch_gicv3.h>
 #include <asm/compat.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuidle.h>
 #include <asm/cacheflush.h>
 #include <asm/exec.h>
 #include <asm/fpsimd.h>
@@ -74,33 +74,6 @@ EXPORT_SYMBOL_GPL(pm_power_off);
 
 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
 
-static void noinstr __cpu_do_idle(void)
-{
-	dsb(sy);
-	wfi();
-}
-
-static void noinstr __cpu_do_idle_irqprio(void)
-{
-	unsigned long pmr;
-	unsigned long daif_bits;
-
-	daif_bits = read_sysreg(daif);
-	write_sysreg(daif_bits | PSR_I_BIT | PSR_F_BIT, daif);
-
-	/*
-	 * Unmask PMR before going idle to make sure interrupts can
-	 * be raised.
-	 */
-	pmr = gic_read_pmr();
-	gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
-
-	__cpu_do_idle();
-
-	gic_write_pmr(pmr);
-	write_sysreg(daif_bits, daif);
-}
-
 /*
  *	cpu_do_idle()
  *
@@ -112,10 +85,14 @@ static void noinstr __cpu_do_idle_irqprio(void)
  */
 void noinstr cpu_do_idle(void)
 {
-	if (system_uses_irq_prio_masking())
-		__cpu_do_idle_irqprio();
-	else
-		__cpu_do_idle();
+	struct arm_cpuidle_context context;
+
+	arm_cpuidle_save_context(&context);
+
+	dsb(sy);
+	wfi();
+
+	arm_cpuidle_restore_context(&context);
 }
 
 /*
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#7 lore raw

PATCH 3/3 PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

In reply to Marc Zyngier

The PSCI CPU suspend code isn't aware of the PMR vs DAIF game, resulting in a system that locks up if entering CPU suspend with GICv3 pNMI enabled.

To save the day, teach the suspend code about our new cpuidle context helpers, which will do everything that's required just like the usual WFI cpuidle code.

This fixes my Altra system, which would otherwise lock-up at boot time when booted with irqchip.gicv3_pseudo_nmi=1.

---
 drivers/firmware/psci/psci.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/firmware/psci/psci.c +5 -0
index 3c1c5daf6df2..d10675bdd9d0 100644
@@ -333,13 +333,18 @@ static int psci_suspend_finisher(unsigned long state)
 
 int psci_cpu_suspend_enter(u32 state)
 {
+	struct arm_cpuidle_context context;
 	int ret;
 
+	arm_cpuidle_save_context(&context);
+
 	if (!psci_power_state_loses_context(state))
 		ret = psci_ops.cpu_suspend(state, 0);
 	else
 		ret = cpu_suspend(state, psci_suspend_finisher);
 
+	arm_cpuidle_restore_context(&context);
+
 	return ret;
 }
 #endif
Signature · 7 lines
2.30.2


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#8 lore raw

Re: [PATCH 3/3] PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

(I see Lorenzo has replied on the other thread including me where the issue was reported asking more details)

Quoted 26 lines · Marc Zyngier

The PSCI CPU suspend code isn't aware of the PMR vs DAIF game, resulting in a system that locks up if entering CPU suspend with GICv3 pNMI enabled.

To save the day, teach the suspend code about our new cpuidle context helpers, which will do everything that's required just like the usual WFI cpuidle code.

This fixes my Altra system, which would otherwise lock-up at boot time when booted with irqchip.gicv3_pseudo_nmi=1.

---
 drivers/firmware/psci/psci.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/firmware/psci/psci.c +1 -0
index 3c1c5daf6df2..d10675bdd9d0 100644
@@ -333,13 +333,18 @@ static int psci_suspend_finisher(unsigned long state)
 
 int psci_cpu_suspend_enter(u32 state)
 {
+	struct arm_cpuidle_context context;
 	int ret;

drivers/firmware/psci/psci.c static int psci_suspend_finisher(unsigned long state) · line 347

Quoted 10 lines
drivers/firmware/psci/psci.c +4 -0
 
+	arm_cpuidle_save_context(&context);
+
 	if (!psci_power_state_loses_context(state))
 		ret = psci_ops.cpu_suspend(state, 0);
 	else
 		ret = cpu_suspend(state, psci_suspend_finisher);
 
+	arm_cpuidle_restore_context(&context);
+

We need similar save/restore for system suspend as well I believe (psci_system_suspend_enter)

--
Regards,
Sudeep

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Tested-by on the v1 series
#10 lore raw
In reply to Marc Zyngier

Hi Marc,

Quoted 8 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

Quoted 9 lines

Instead of spreading more magic code around, I've elected to provide a pair of helpers (arm_cpuidle_{save,restore}_context()) which do the heavy lifting.

With that in place, I can finally boot the above system with irqchip.gicv3_pseudo_nmi=1. I'd welcome feedback from people who may have experienced similar issues in the past (and on different machines).

With pNMIs my Ampere eMAG would always hang at boot, and your patches make it actually survive. I briefly kicked perf to get some PMU IRQs and that also seems to be going just fine. Thanks for digging into this mess!

Quoted 5 lines

Marc Zyngier (3):
arm64: Add cpuidle context save/restore helpers
arm64: Convert cpu_do_idle() to using cpuidle context helpers
PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

Quoted 8 lines
 arch/arm/include/asm/cpuidle.h   |  5 ++++
 arch/arm64/include/asm/cpuidle.h | 35 +++++++++++++++++++++++++++
 arch/arm64/kernel/process.c      | 41 +++++++-------------------------
 drivers/firmware/psci/psci.c     |  5 ++++
 4 files changed, 54 insertions(+), 32 deletions(-)

--
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Tested-by on the v1 series
#11 lore raw
In reply to Marc Zyngier

Hi Marc,

Quoted 8 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

Quoted 9 lines

Instead of spreading more magic code around, I've elected to provide a pair of helpers (arm_cpuidle_{save,restore}_context()) which do the heavy lifting.

With that in place, I can finally boot the above system with irqchip.gicv3_pseudo_nmi=1. I'd welcome feedback from people who may have experienced similar issues in the past (and on different machines).

With pNMIs my Ampere eMAG would always hang at boot, and your patches make it actually survive. I briefly kicked perf to get some PMU IRQs and that also seems to be going just fine. Thanks for digging into this mess!

Quoted 5 lines

Marc Zyngier (3):
arm64: Add cpuidle context save/restore helpers
arm64: Convert cpu_do_idle() to using cpuidle context helpers
PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

Quoted 8 lines
 arch/arm/include/asm/cpuidle.h   |  5 ++++
 arch/arm64/include/asm/cpuidle.h | 35 +++++++++++++++++++++++++++
 arch/arm64/kernel/process.c      | 41 +++++++-------------------------
 drivers/firmware/psci/psci.c     |  5 ++++
 4 files changed, 54 insertions(+), 32 deletions(-)

--
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#12 lore raw
In reply to Marc Zyngier
Quoted 7 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

After investigating a bit I realised that this should depend on ICC_CTLR_EL3.PMHE - if that's clear the PMR should not affect the GICR->CPU IRQ forwarding (or WakeRequest signal generation when the GICR_WAKER.ProcessorSleep==1).

IIUC if PMHE == 0, the PMR plays no role in wfi completion (and WakeSignal generation for a CPU/GICR in quiescent state).

I assume on Ampere Altra PMHE == 1.

This changes almost nothing to the need for this patchset but at least we clarify this behaviour.

Also, we should not be writing ICC_PMR_EL1 when GICR_WAKER.ProcessorSleep == 1 (which may be set in gic_cpu_pm_notifier()), this can hang the system.

I wonder whether this arm_cpuidle_{save,restore}_context() should be moved into the gic_cpu_pm_notifier() itself - which would solve also the PSCI suspend issue Sudeep raised - it would be a bit ugly though (CPU PM notifiers are run in S2R and CPUidle automatically and this would work for any S2R/CPUidle backend other than PSCI even though that does not/will never exist on arm64 ;-))

https://lore.kernel.org/linux-arm-kernel/20210608182044.ayqa6fbab4jyz7kp@bogus

I still believe this series is right - just raised these points for discussion.

Thanks,
Lorenzo

Quoted 14 lines

Instead of spreading more magic code around, I've elected to provide a pair of helpers (arm_cpuidle_{save,restore}_context()) which do the heavy lifting.

With that in place, I can finally boot the above system with irqchip.gicv3_pseudo_nmi=1. I'd welcome feedback from people who may have experienced similar issues in the past (and on different machines).

Marc Zyngier (3):
arm64: Add cpuidle context save/restore helpers
arm64: Convert cpu_do_idle() to using cpuidle context helpers
PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

Quoted 9 lines
 arch/arm/include/asm/cpuidle.h   |  5 ++++
 arch/arm64/include/asm/cpuidle.h | 35 +++++++++++++++++++++++++++
 arch/arm64/kernel/process.c      | 41 +++++++-------------------------
 drivers/firmware/psci/psci.c     |  5 ++++
 4 files changed, 54 insertions(+), 32 deletions(-)
Signature · 1 line
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#13 lore raw
Quoted 9 lines · Lorenzo Pieralisi
Quoted 7 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

[...]

Quoted 4 lines

I wonder whether this arm_cpuidle_{save,restore}_context() should be moved into the gic_cpu_pm_notifier() itself - which would solve also the PSCI suspend issue Sudeep raised - it would be a bit ugly though (CPU PM notifiers are run in S2R and CPUidle

+1 if possible, I hadn't fully understood the issue to make this suggestion. But yes if possible, we must to honour the abstraction even though PSCI is the only user 😄.

Quoted 4 lines

automatically and this would work for any S2R/CPUidle backend other than PSCI even though that does not/will never exist on arm64 ;-))

I almost wrote the same thing in my earlier email and deleted 😉 before sending.

--
Regards,
Sudeep

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#15 lore raw
In reply to Lorenzo Pieralisi

Hi Lorenzo,

Quoted 6 lines · Lorenzo Pieralisi
Quoted 4 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

Quoted 8 lines
Quoted 3 lines

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

After investigating a bit I realised that this should depend on ICC_CTLR_EL3.PMHE - if that's clear the PMR should not affect the GICR->CPU IRQ forwarding (or WakeRequest signal generation when the GICR_WAKER.ProcessorSleep==1).

You lost me here. I don't see what PMHE has to do here. It is solely used for 1:N distribution, and is the only way PMR does affect the propagation of interrupts to the CPU interface. Fortunately, nobody uses 1:N.

Quoted 2 lines

IIUC if PMHE == 0, the PMR plays no role in wfi completion (and WakeSignal generation for a CPU/GICR in quiescent state).

Of course it does. PMR gates interrupts *before* they are signalled to the CPU, meaning that if you keep interrupt masked at the PMR level, you will never wake up from WFI. Or am I missing your point entirely?

Quoted 2 lines

I assume on Ampere Altra PMHE == 1.

No, it is 0, as indicated by:

<quote>

[    0.000000] GICv3: Pseudo-NMIs enabled using relaxed ICC_PMR_EL1 synchronisation

</quote>

Quoted 6 lines

This changes almost nothing to the need for this patchset but at least we clarify this behaviour.

Also, we should not be writing ICC_PMR_EL1 when GICR_WAKER.ProcessorSleep == 1 (which may be set in gic_cpu_pm_notifier()), this can hang the system.

Why? PMR defines what interrupts will be presented to the CPU interface and trigger an exception. It doesn't affect putting the CPU to sleep nor the wake-up.

Quoted 3 lines

I wonder whether this arm_cpuidle_{save,restore}_context() should be moved into the gic_cpu_pm_notifier() itself - which would solve also the PSCI suspend issue Sudeep raised - it would be

Moving from PMR to DAIF masking is something we only do on particular spots (exception entry/exit, guest entry/exit) as it affects the behaviour of simple things such as local_irq_*(). Moving it to a higher level feels super dangerous.

Quoted 9 lines

a bit ugly though (CPU PM notifiers are run in S2R and CPUidle automatically and this would work for any S2R/CPUidle backend other than PSCI even though that does not/will never exist on arm64 ;-))

https://lore.kernel.org/linux-arm-kernel/20210608182044.ayqa6fbab4jyz7kp@bogus

I still believe this series is right - just raised these points for discussion.

Thanks,

	M.
Signature · 6 lines
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#14 lore raw
In reply to Sudeep Holla

Hi Sudeep,

Quoted 14 lines · Sudeep Holla
Quoted 9 lines · Lorenzo Pieralisi
Quoted 7 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

[...]

Quoted 8 lines
Quoted 4 lines

I wonder whether this arm_cpuidle_{save,restore}_context() should be moved into the gic_cpu_pm_notifier() itself - which would solve also the PSCI suspend issue Sudeep raised - it would be a bit ugly though (CPU PM notifiers are run in S2R and CPUidle

+1 if possible, I hadn't fully understood the issue to make this suggestion. But yes if possible, we must to honour the abstraction even though PSCI is the only user 😄.

See my reply to Lorenzo. Once we switch from PMR to DAIF masking, none of the local interrupt control helpers work anymore. If we start leaking this change of behaviour at a higher level in the stack, I have no idea what happens anymore.

Which is why we perform such switching in very localised cases such as exception, guest entry, and (oh surprise! ;-) cpu_do_idle().

Thanks,

	M.
Signature · 6 lines
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#16 lore raw
In reply to Marc Zyngier
Quoted 24 lines · Marc Zyngier

Hi Lorenzo,

Quoted 6 lines · Lorenzo Pieralisi
Quoted 4 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

Quoted 8 lines
Quoted 3 lines

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

After investigating a bit I realised that this should depend on ICC_CTLR_EL3.PMHE - if that's clear the PMR should not affect the GICR->CPU IRQ forwarding (or WakeRequest signal generation when the GICR_WAKER.ProcessorSleep==1).

You lost me here. I don't see what PMHE has to do here. It is solely used for 1:N distribution, and is the only way PMR does affect the propagation of interrupts to the CPU interface. Fortunately, nobody uses 1:N.

Quoted 6 lines
Quoted 2 lines

IIUC if PMHE == 0, the PMR plays no role in wfi completion (and WakeSignal generation for a CPU/GICR in quiescent state).

Of course it does. PMR gates interrupts *before* they are signalled to the CPU, meaning that if you keep interrupt masked at the PMR level, you will never wake up from WFI. Or am I missing your point entirely?

For "simple" wfi (as in executing the wfi instruction) yes. The IRQs are forwarded to the CPU interface that filters the IRQs based on priorities and signal the I/F "pin" so that the core wakes up and wfi completes - forgive me my misunderstanding.

For deep sleep states where GICR_WAKER.ProcessorSleep == 1, the WakeSignal (ie CPU reset) is generated independently of the PMR value AFAIK. This means that even *if* an IRQ is supposed to be masked by the PMR it would wake up a sleeping core _anyway_.

This behaviour is different from shallow C-states (and simple wfi).

That's why I asked what path is causing trouble in psci_cpu_suspend_enter().

Quoted 11 lines
Quoted 1 line

I assume on Ampere Altra PMHE == 1.

No, it is 0, as indicated by:

<quote>

[    0.000000] GICv3: Pseudo-NMIs enabled using relaxed ICC_PMR_EL1 synchronisation

</quote>

Quoted 3 lines

This changes almost nothing to the need for this patchset but at least we clarify this behaviour.

Quoted 7 lines
Quoted 3 lines

Also, we should not be writing ICC_PMR_EL1 when GICR_WAKER.ProcessorSleep == 1 (which may be set in gic_cpu_pm_notifier()), this can hang the system.

Why? PMR defines what interrupts will be presented to the CPU interface and trigger an exception. It doesn't affect putting the CPU to sleep nor the wake-up.

I don't think we are allowed to have traffic between the CPU IF and the GICR when ProcessorSleep == 1. So, again IIUC, we can't write the PMR (if PMHE == 1) after putting the GICR in ProcessorSleep ==1 because this would sync with the GICR.

Quoted 8 lines
Quoted 3 lines

I wonder whether this arm_cpuidle_{save,restore}_context() should be moved into the gic_cpu_pm_notifier() itself - which would solve also the PSCI suspend issue Sudeep raised - it would be

Moving from PMR to DAIF masking is something we only do on particular spots (exception entry/exit, guest entry/exit) as it affects the behaviour of simple things such as local_irq_*(). Moving it to a higher level feels super dangerous.

Yes it is dangerous and ugly. It is also true that what needs to be saved/restore on idle entry is becoming extremely complicated and hard to track across kernel subsystems and that's unfortunate too, that's what is causing these bugs.

Thanks,
Lorenzo

Quoted 5 lines
Quoted 5 lines

a bit ugly though (CPU PM notifiers are run in S2R and CPUidle automatically and this would work for any S2R/CPUidle backend other than PSCI even though that does not/will never exist on arm64 ;-))

Quoted 11 lines
Quoted 4 lines

https://lore.kernel.org/linux-arm-kernel/20210608182044.ayqa6fbab4jyz7kp@bogus

I still believe this series is right - just raised these points for discussion.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#17 lore raw
Quoted 29 lines · Lorenzo Pieralisi
Quoted 27 lines · Marc Zyngier

Hi Lorenzo,

Quoted 6 lines · Lorenzo Pieralisi
Quoted 4 lines · Marc Zyngier

It appears that although cpu_do_idle() is correctly dealing with the PMR/DAIF duality, the PSCI cpu-suspend code has been left unaware of it.

Quoted 8 lines
Quoted 3 lines

On a system that uses PSCI for idle (such as the Ampere Altra I have access to), the kernel dies as soon as it enters idle (interrupts are off at the GIC CPU interface level). Boo.

After investigating a bit I realised that this should depend on ICC_CTLR_EL3.PMHE - if that's clear the PMR should not affect the GICR->CPU IRQ forwarding (or WakeRequest signal generation when the GICR_WAKER.ProcessorSleep==1).

You lost me here. I don't see what PMHE has to do here. It is solely used for 1:N distribution, and is the only way PMR does affect the propagation of interrupts to the CPU interface. Fortunately, nobody uses 1:N.

Quoted 2 lines

IIUC if PMHE == 0, the PMR plays no role in wfi completion (and WakeSignal generation for a CPU/GICR in quiescent state).

Quoted 8 lines
Quoted 3 lines

Of course it does. PMR gates interrupts *before* they are signalled to the CPU, meaning that if you keep interrupt masked at the PMR level, you will never wake up from WFI. Or am I missing your point entirely?

For "simple" wfi (as in executing the wfi instruction) yes. The IRQs are forwarded to the CPU interface that filters the IRQs based on priorities and signal the I/F "pin" so that the core wakes up and wfi completes - forgive me my misunderstanding.

No worries. We're talking about the GIC architecture here, which has the potential to confuse anyone! :D

Quoted 4 lines

For deep sleep states where GICR_WAKER.ProcessorSleep == 1, the WakeSignal (ie CPU reset) is generated independently of the PMR value AFAIK. This means that even *if* an IRQ is supposed to be masked by the PMR it would wake up a sleeping core _anyway_.

I'm not sure we can draw this conclusion. It certainly isn't the behaviour I'm observing. Otherwise, my system would be able to wake-up without any additional hacks. It may wake-up the CPU interface, but not the whole core. It is also completely possible that firmware will use the PMR value as a hint to decide which interrupts can wake up the CPU if it is so inclined.

Quoted 4 lines

This behaviour is different from shallow C-states (and simple wfi).

That's why I asked what path is causing trouble in psci_cpu_suspend_enter().

It is the one where we don't loose context. Which makes sense, as it would otherwise behave like a full reset, and we'd end-up with some sane values.

Quoted 15 lines
Quoted 15 lines
Quoted 1 line

I assume on Ampere Altra PMHE == 1.

No, it is 0, as indicated by:

<quote>

[    0.000000] GICv3: Pseudo-NMIs enabled using relaxed ICC_PMR_EL1 synchronisation

</quote>

Quoted 6 lines

This changes almost nothing to the need for this patchset but at least we clarify this behaviour.

Also, we should not be writing ICC_PMR_EL1 when GICR_WAKER.ProcessorSleep == 1 (which may be set in gic_cpu_pm_notifier()), this can hang the system.

Quoted 8 lines
Quoted 3 lines

Why? PMR defines what interrupts will be presented to the CPU interface and trigger an exception. It doesn't affect putting the CPU to sleep nor the wake-up.

I don't think we are allowed to have traffic between the CPU IF and the GICR when ProcessorSleep == 1. So, again IIUC, we can't write the PMR (if PMHE == 1) after putting the GICR in ProcessorSleep ==1 because this would sync with the GICR.

Hmmm. This restriction isn't obvious to me. There is some vague threats in 10.1, but nothing concrete. A.4.11 is more explicit, but doesn't really define what 'traffic' is. It is also tied to the stream protocol, which isn't actually mandated.

But if it exists, PHME doesn't have any influence on PMR being sent back to the RD. That can happen at any time (it is just that we enforce it with a DSB in the case where PHME is set). Note that do_cpu_idle() already does this, so we'd already be on thin ice. It is also unclear whether 'traffic' occurs when the group enables are set to 0, which is the case when we set ProcessorSleep=1.

And to be honest, setting ProcessorSleep=1 in the kernel (which implies DS=1) has always been pretty odd. I don't know of any HW that requires it. I'd be tempted to get rid of it for good.

Thanks,

	M.
Signature · 6 lines
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] arm64: Add cpuidle context save/restore helpers

In reply to Marc Zyngier
Quoted 6 lines · Marc Zyngier

As we need to start doing some additional work on all idle paths, let's introduce a set of macros that will perform the work related to the GICv3 pseudo-NMI idle entry exit.

Stubs are introduced to 32bit ARM for compatibility. As these helpers are currently unused, the is no functional

s/the/there

Quoted 56 lines

change.

---
 arch/arm/include/asm/cpuidle.h   |  5 +++++
 arch/arm64/include/asm/cpuidle.h | 35 ++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/arm/include/asm/cpuidle.h +5 -0
index 0d67ed682e07..1e0b8da12d96 100644
@@ -49,4 +49,9 @@ extern int arm_cpuidle_suspend(int index);
 
 extern int arm_cpuidle_init(int cpu);
 
+struct arm_cpuidle_context { };
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+
 #endif
arch/arm64/include/asm/cpuidle.h +26 -0
index 3c5ddb429ea2..53adad0a5c7e 100644
@@ -18,4 +18,39 @@ static inline int arm_cpuidle_suspend(int index)
 	return -EOPNOTSUPP;
 }
 #endif
+
+#ifdef CONFIG_ARM64_PSEUDO_NMI
+#include <asm/arch_gicv3.h>
+
+struct arm_cpuidle_context {
+	unsigned long pmr;
+	unsigned long daif_bits;
+};
+
+#define arm_cpuidle_save_context(__c)					\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			c->daif_bits = read_sysreg(daif);		\
+			write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \
+				     daif);				\
+			c->pmr = gic_read_pmr();			\
+			gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \
+		}							\
+	} while (0)
+
+#define arm_cpuidle_restore_context(__c)				\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			gic_write_pmr(c->pmr);				\

arch/arm64/include/asm/cpuidle.h static inline int arm_cpuidle_suspend(int index) · line 56

Quoted 10 lines
arch/arm64/include/asm/cpuidle.h +9 -0
+			write_sysreg(c->daif_bits, daif);		\
+		}							\
+	} while (0)
+#else
+struct arm_cpuidle_context { };
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+#endif
 #endif

It looks good to me - maybe I would define it irq_context for clarity but that's just a naming convention.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] arm64: Convert cpu_do_idle() to using cpuidle context helpers

In reply to Marc Zyngier
Quoted 10 lines · Marc Zyngier

Now that we have helpers that are aware of the pseudo-NMI feature, introduce them to cpu_do_idle(). This allows for some nice cleanup.

No functional change intended.

---
 arch/arm64/kernel/process.c | 41 ++++++++-----------------------------
 1 file changed, 9 insertions(+), 32 deletions(-)
Quoted 71 lines
arch/arm64/kernel/process.c +9 -32
index b4bb67f17a2c..f99144eda8dc 100644
@@ -46,9 +46,9 @@
 #include <linux/prctl.h>
 
 #include <asm/alternative.h>
-#include <asm/arch_gicv3.h>
 #include <asm/compat.h>
 #include <asm/cpufeature.h>
+#include <asm/cpuidle.h>
 #include <asm/cacheflush.h>
 #include <asm/exec.h>
 #include <asm/fpsimd.h>
@@ -74,33 +74,6 @@ EXPORT_SYMBOL_GPL(pm_power_off);
 
 void (*arm_pm_restart)(enum reboot_mode reboot_mode, const char *cmd);
 
-static void noinstr __cpu_do_idle(void)
-{
-	dsb(sy);
-	wfi();
-}
-
-static void noinstr __cpu_do_idle_irqprio(void)
-{
-	unsigned long pmr;
-	unsigned long daif_bits;
-
-	daif_bits = read_sysreg(daif);
-	write_sysreg(daif_bits | PSR_I_BIT | PSR_F_BIT, daif);
-
-	/*
-	 * Unmask PMR before going idle to make sure interrupts can
-	 * be raised.
-	 */
-	pmr = gic_read_pmr();
-	gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET);
-
-	__cpu_do_idle();
-
-	gic_write_pmr(pmr);
-	write_sysreg(daif_bits, daif);
-}
-
 /*
  *	cpu_do_idle()
  *
@@ -112,10 +85,14 @@ static void noinstr __cpu_do_idle_irqprio(void)
  */
 void noinstr cpu_do_idle(void)
 {
-	if (system_uses_irq_prio_masking())
-		__cpu_do_idle_irqprio();
-	else
-		__cpu_do_idle();
+	struct arm_cpuidle_context context;
+
+	arm_cpuidle_save_context(&context);
+
+	dsb(sy);
+	wfi();
+
+	arm_cpuidle_restore_context(&context);
 }
 
 /*
Signature · 1 line
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] PSCI: Use cpuidle context helpers in psci_cpu_suspend_enter()

In reply to Marc Zyngier
Quoted 8 lines · Marc Zyngier

The PSCI CPU suspend code isn't aware of the PMR vs DAIF game, resulting in a system that locks up if entering CPU suspend with GICv3 pNMI enabled.

To save the day, teach the suspend code about our new cpuidle context helpers, which will do everything that's required just like the usual WFI cpuidle code.

Quoted 7 lines

This fixes my Altra system, which would otherwise lock-up at boot time when booted with irqchip.gicv3_pseudo_nmi=1.

---
 drivers/firmware/psci/psci.c | 5 +++++
 1 file changed, 5 insertions(+)

We need an additional patch for PSCI suspend, regardless:

Quoted 26 lines
drivers/firmware/psci/psci.c +5 -0
index 3c1c5daf6df2..d10675bdd9d0 100644
@@ -333,13 +333,18 @@ static int psci_suspend_finisher(unsigned long state)
 
 int psci_cpu_suspend_enter(u32 state)
 {
+	struct arm_cpuidle_context context;
 	int ret;
 
+	arm_cpuidle_save_context(&context);
+
 	if (!psci_power_state_loses_context(state))
 		ret = psci_ops.cpu_suspend(state, 0);
 	else
 		ret = cpu_suspend(state, psci_suspend_finisher);
 
+	arm_cpuidle_restore_context(&context);
+
 	return ret;
 }
 #endif
Signature · 1 line
2.30.2

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw

Re: [PATCH 1/3] arm64: Add cpuidle context save/restore helpers

In reply to Lorenzo Pieralisi
Quoted 72 lines · Lorenzo Pieralisi
Quoted 6 lines · Marc Zyngier

As we need to start doing some additional work on all idle paths, let's introduce a set of macros that will perform the work related to the GICv3 pseudo-NMI idle entry exit.

Stubs are introduced to 32bit ARM for compatibility. As these helpers are currently unused, the is no functional

s/the/there

Quoted 61 lines

change.

---
 arch/arm/include/asm/cpuidle.h   |  5 +++++
 arch/arm64/include/asm/cpuidle.h | 35 ++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/arm/include/asm/cpuidle.h +5 -0
index 0d67ed682e07..1e0b8da12d96 100644
@@ -49,4 +49,9 @@ extern int arm_cpuidle_suspend(int index);
 
 extern int arm_cpuidle_init(int cpu);
 
+struct arm_cpuidle_context { };
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+
 #endif
arch/arm64/include/asm/cpuidle.h +31 -0
index 3c5ddb429ea2..53adad0a5c7e 100644
@@ -18,4 +18,39 @@ static inline int arm_cpuidle_suspend(int index)
 	return -EOPNOTSUPP;
 }
 #endif
+
+#ifdef CONFIG_ARM64_PSEUDO_NMI
+#include <asm/arch_gicv3.h>
+
+struct arm_cpuidle_context {
+	unsigned long pmr;
+	unsigned long daif_bits;
+};
+
+#define arm_cpuidle_save_context(__c)					\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			c->daif_bits = read_sysreg(daif);		\
+			write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \
+				     daif);				\
+			c->pmr = gic_read_pmr();			\
+			gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \
+		}							\
+	} while (0)
+
+#define arm_cpuidle_restore_context(__c)				\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			gic_write_pmr(c->pmr);				\
+			write_sysreg(c->daif_bits, daif);		\
+		}							\
+	} while (0)
+#else
+struct arm_cpuidle_context { };
Quoted 8 lines

arch/arm64/include/asm/cpuidle.h static inline int arm_cpuidle_suspend(int index) · line 56

Quoted 5 lines
arch/arm64/include/asm/cpuidle.h +4 -0
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+#endif
 #endif

It looks good to me - maybe I would define it irq_context for clarity but that's just a naming convention.

would:

struct arm_cpuidle_irq_context { ... };
#define arm_cpuidle_save_irq_context(c) ...
#define arm_cpuidle_restore_irq_context(c) ...

be OK for you?

Quoted 1 line

Thanks!

	M.
Signature · 6 lines
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#4 lore raw

Re: [PATCH 1/3] arm64: Add cpuidle context save/restore helpers

Quoted 83 lines · Marc Zyngier
Quoted 72 lines · Lorenzo Pieralisi
Quoted 6 lines · Marc Zyngier

As we need to start doing some additional work on all idle paths, let's introduce a set of macros that will perform the work related to the GICv3 pseudo-NMI idle entry exit.

Stubs are introduced to 32bit ARM for compatibility. As these helpers are currently unused, the is no functional

s/the/there

Quoted 61 lines

change.

---
 arch/arm/include/asm/cpuidle.h   |  5 +++++
 arch/arm64/include/asm/cpuidle.h | 35 ++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+)
arch/arm/include/asm/cpuidle.h +5 -0
index 0d67ed682e07..1e0b8da12d96 100644
@@ -49,4 +49,9 @@ extern int arm_cpuidle_suspend(int index);
 
 extern int arm_cpuidle_init(int cpu);
 
+struct arm_cpuidle_context { };
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+
 #endif
arch/arm64/include/asm/cpuidle.h +31 -0
index 3c5ddb429ea2..53adad0a5c7e 100644
@@ -18,4 +18,39 @@ static inline int arm_cpuidle_suspend(int index)
 	return -EOPNOTSUPP;
 }
 #endif
+
+#ifdef CONFIG_ARM64_PSEUDO_NMI
+#include <asm/arch_gicv3.h>
+
+struct arm_cpuidle_context {
+	unsigned long pmr;
+	unsigned long daif_bits;
+};
+
+#define arm_cpuidle_save_context(__c)					\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			c->daif_bits = read_sysreg(daif);		\
+			write_sysreg(c->daif_bits | PSR_I_BIT | PSR_F_BIT, \
+				     daif);				\
+			c->pmr = gic_read_pmr();			\
+			gic_write_pmr(GIC_PRIO_IRQON | GIC_PRIO_PSR_I_SET); \
+		}							\
+	} while (0)
+
+#define arm_cpuidle_restore_context(__c)				\
+	do {								\
+		struct arm_cpuidle_context *c = __c;			\
+		if (system_uses_irq_prio_masking()) {			\
+			gic_write_pmr(c->pmr);				\
+			write_sysreg(c->daif_bits, daif);		\
+		}							\
+	} while (0)
+#else
+struct arm_cpuidle_context { };
Quoted 8 lines

arch/arm64/include/asm/cpuidle.h static inline int arm_cpuidle_suspend(int index) · line 56

Quoted 5 lines
arch/arm64/include/asm/cpuidle.h +4 -0
+
+#define arm_cpuidle_save_context(c)	(void)c
+#define arm_cpuidle_restore_context(c)	(void)c
+#endif
 #endif

It looks good to me - maybe I would define it irq_context for clarity but that's just a naming convention.

Quoted 7 lines

would:

struct arm_cpuidle_irq_context { ... };
#define arm_cpuidle_save_irq_context(c) ...
#define arm_cpuidle_restore_irq_context(c) ...

be OK for you?

Yes absolutely, thanks a lot.

Lorenzo

Quoted 8 lines
Quoted 1 line

Thanks!

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel