Skip to contentSkip to conversation

Fix long-standing issues in legacy exynos-cpuidle driver

PATCH New Marek Szyprowski 1 participant 5 messages Dec 3 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20261203225028eucas1p2ad24ece9045ced1992d0b69fe1eed162@eucas1p2.samsung.com

#1 lore raw

v2 cover Fix long-standing issues in legacy exynos-cpuidle driver

Hi All,

I had some spare LLM tokens to burn, so I decided to investigate the long-standing issues in the legacy Exynos CPU-idle code. This patchset is a result of that. Most issues were related to the Exynos AFTR (ARM Off, Top Running) handling. They lead to random memory corruption, crashes and RCU warnings on Exynos3250, Exynos4210, Exynos4212/4412 and Exynos5250 based boards. They show up mainly in CPU-idle stress tests, in CPU hotplug tests and after a system suspend/resume cycle.

Patch #1 fixes the coupled AFTR mode (Exynos3250 and Exynos4210). There CPU1 was powered down while its data cache and SMP coherency were still enabled, so dirty cache lines migrated to it were lost.

Patch #2 restores the ARM_CORE_OPTION registers of the secondary CPUs after system suspend on Exynos4210. Without this the coupled AFTR mode stops working after resume.

Patch #3 fixes the non-coupled AFTR mode (Exynos4212, Exynos4412 and Exynos5250). Checking num_online_cpus() is not enough to know that the other cores are really powered down during CPU hotplug, so the patch checks their power state in the PMU before entering AFTR.

Patch #4 fixes the 'RCU not watching' warnings. These have been triggered since cpu_pm_enter()/cpu_pm_exit() stopped handling RCU on their own. The patch marks the AFTR states with CPUIDLE_FLAG_RCU_IDLE and calls ct_cpuidle_enter()/ct_cpuidle_exit() only around cpu_suspend().

The patches are ordered to ease backporting to the stable kernels. Patches #1-#3 fix issues that go back to v4.x and don't depend on the RCU idle rework, so they apply to all the maintained stable trees. Patch #4 fixes an issue introduced in v6.3, so it comes last and is needed only in v6.3+ trees.

Note: these patches have been developed with the help of an LLM-based coding assistant under author's supervision. All of them have been reviewed and tested on real hardware: Trats (Exynos4210), Trats2 (Exynos4412), Rinato (Exynos3250) and Snow (Exynos5250).

Best regards
Marek Szyprowski

Changelog:
v2:
- patch #3: propagate the result of exynos_enter_aftr() in exynos_cpu0_enter_aftr() (coupled cpuidle driver), so the AFTR entry rejected because CPU1 is still powered is no longer reported to the cpuidle core as a successful C1 state transition (issue found by the sashiko review bot), extended the commit message accordingly

v1: https://lore.kernel.org/all/[email protected]/

Patch summary:

Marek Szyprowski (4):
ARM: exynos: Exit coherency before powering down CPU1 in coupled AFTR
ARM: exynos: Preserve ARM core options across suspend
ARM: exynos: Enter AFTR only when other cores are powered down
cpuidle: exynos: Let AFTR state handle RCU idle itself

 arch/arm/mach-exynos/common.h    |  2 +-
 arch/arm/mach-exynos/pm.c        | 73 +++++++++++++++++++++++++++++---
 arch/arm/mach-exynos/suspend.c   | 29 +++++++++++++
 drivers/cpuidle/cpuidle-exynos.c | 22 ++++++----
 4 files changed, 112 insertions(+), 14 deletions(-)
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 1/4 ARM: exynos: Exit coherency before powering down CPU1 in coupled AFTR

In the coupled AFTR mode, CPU1 is powered down from exynos_wfi_finisher() with the data cache and the SMP coherency still enabled. Between the cache flush and the actual power down, dirty cache lines might be migrated from CPU0 to the CPU1 L1 cache by the coherency logic. Those lines are then lost together with the CPU1 power, what leads to random memory corruption, like the following crash observed on Exynos3250 based Rinato board during a cpuidle stress test:

Unable to handle kernel NULL pointer dereference at virtual address 00000010 when read

...
PC is at __lock_acquire+0x4c/0x2a14
LR is at lock_acquire+0x140/0x3d8
...
lock_acquire from _raw_spin_lock+0x38/0x48 _raw_spin_lock from pte_offset_map_lock+0x74/0x134 pte_offset_map_lock from filemap_map_pages+0x1d4/0xa1c
...

Fix this by disabling the data cache, flushing it and exiting coherency with v7_exit_coherency_flush() before entering WFI, like it is already done in the CPU hotplug code. Flush all cache levels on Exynos3250 like before, while on Exynos4210 flushing L1 is enough, as its L2 is the outer PL310 cache. Restore the coherency and data cache if the power down is aborted.

Fixes 712eddf70225 ("cpuidle: exynos: add coupled cpuidle support for exynos4210")
---
 arch/arm/mach-exynos/pm.c | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)
arch/arm/mach-exynos/pm.c +25 -1
index 0019d21bff90..6806af6fcb0b 100644
@@ -21,6 +21,7 @@
 #include <asm/smp_scu.h>
 #include <asm/suspend.h>
 #include <asm/cacheflush.h>
+#include <asm/cp15.h>
 
 #include "common.h"
 
@@ -278,10 +279,33 @@ static int exynos_cpu0_enter_aftr(void)
 
 static int exynos_wfi_finisher(unsigned long flags)
 {
+	unsigned int v;
+
+	/*
+	 * Disable D-cache and exit coherency before powering down, otherwise
+	 * dirty cache lines migrated from the other cpu might get lost.
+	 */
 	if (soc_is_exynos3250())
-		flush_cache_all();
+		v7_exit_coherency_flush(all);
+	else
+		v7_exit_coherency_flush(louis);
+
 	cpu_do_idle();
 
+	/* Power down has been aborted, restore coherency and D-cache */
+	asm volatile(
+	"	mrc	p15, 0, %0, c1, c0, 1\n"
+	"	orr	%0, %0, %2\n"
+	"	mcr	p15, 0, %0, c1, c0, 1\n"
+	"	isb\n"
+	"	mrc	p15, 0, %0, c1, c0, 0\n"
+	"	orr	%0, %0, %1\n"
+	"	mcr	p15, 0, %0, c1, c0, 0\n"
+	"	isb\n"
+	  : "=&r" (v)
+	  : "Ir" (CR_C), "Ir" (0x40)
+	  : "cc");
+
 	return -1;
 }
 
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 2/4 ARM: exynos: Preserve ARM core options across suspend

In reply to Marek Szyprowski

Exynos4210 loses the ARM_CORE_OPTION registers for secondary CPUs during system sleep. This clears the USE_STANDBYWFI and USE_STANDBYWFE bits and prevents coupled AFTR idle from working after resume.

Save the option registers for all possible CPUs before suspend and restore them on resume so coupled cpuidle remains operational after system suspend/resume cycle.

Fixes 712eddf70225 ("cpuidle: exynos: add coupled cpuidle support for exynos4210")
---
 arch/arm/mach-exynos/suspend.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
arch/arm/mach-exynos/suspend.c +29 -0
index 22d723553f62..6a436c64020a 100644
@@ -375,9 +375,36 @@ static void exynos5420_pm_prepare(void)
 	pmu_raw_writel(tmp, EXYNOS5420_PSGEN_OPTION);
 }
 
+/*
+ * The ARM_CORE_OPTION registers of the secondary CPUs are not retained
+ * across the system sleep on Exynos4210. The coupled AFTR mode relies on
+ * the USE_STANDBYWFI and USE_STANDBYWFE bits set there, so save and restore
+ * them.
+ */
+static u32 exynos_core_option[CONFIG_NR_CPUS];
+
+static void exynos_pm_save_core_option(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < num_possible_cpus(); i++)
+		exynos_core_option[i] =
+			pmu_raw_readl(EXYNOS_ARM_CORE_OPTION(i));
+}
+
+static void exynos_pm_restore_core_option(void)
+{
+	unsigned int i;
+
+	for (i = 0; i < num_possible_cpus(); i++)
+		pmu_raw_writel(exynos_core_option[i],
+			       EXYNOS_ARM_CORE_OPTION(i));
+}
 
 static int exynos_pm_suspend(void *data)
 {
+	exynos_pm_save_core_option();
+
 	exynos_pm_central_suspend();
 
 	/* Setting SEQ_OPTION register */
@@ -412,6 +439,8 @@ static void exynos_pm_resume(void *data)
 {
 	u32 cpuid = read_cpuid_part();
 
+	exynos_pm_restore_core_option();
+
 	if (exynos_pm_central_resume())
 		goto early_wakeup;
 
Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 3/4 ARM: exynos: Enter AFTR only when other cores are powered down

In reply to Marek Szyprowski

The non-coupled Exynos cpuidle driver (used on Exynos4212, Exynos4412 and Exynos5250) lets CPU0 enter AFTR mode when num_online_cpus() == 1. This is not enough to ensure that the other cores are really powered down:

- during CPU hotplug-in, exynos_boot_secondary() returns as soon as the secondary core leaves the holding pen, long before it marks itself online, so CPU0 goes idle in __cpu_up() and enters AFTR while the secondary core is already running with caches enabled,

- during CPU hotplug-out, the core is marked offline long before it reaches WFI in exynos_cpu_die() and there is no .cpu_kill callback to wait for it, so CPU0 can enter AFTR while the dying core is still flushing its caches.

This leads to memory corruption or bus errors, like the following crash observed on Exynos4412-based Trats2 board with multi_v7_defconfig (with PL310_ERRATA_769419 enabled, arch_cpu_idle_enter() accesses the L2 cache controller) during the CPU hotplug test:

Unhandled fault: imprecise external abort (0x1c06) at 0x00018000 [00018000] *pgd=00000000

Internal error: : 1c06 [#1] SMP ARM

...
PC is at cpuidle_get_cpu_driver+0x0/0x28
LR is at do_idle+0xc4/0x340
...
cpuidle_get_cpu_driver from do_idle+0xc4/0x340 do_idle from cpu_startup_entry+0x28/0x2c cpu_startup_entry from secondary_start_kernel+0x114/0x11c secondary_start_kernel from 0x4031d0a4

Fix this by checking the power state of the other cores in the PMU before entering AFTR mode and falling back to WFI if any of them is still powered. Only the current CPU can power up a secondary core when it is the only online one, so the check is not racy.

The same situation can happen in the coupled cpuidle driver (used on Exynos3250 and Exynos4210): when CPU1 is already marked offline but is still powered, exynos_cpu0_enter_aftr() skips waiting for it. Propagate the error returned by exynos_enter_aftr() there, so cpuidle core doesn't account the rejected AFTR entry as a successful C1 state transition.

Fixes 118f5c1d5518 ("ARM: EXYNOS: cpuidle: fix AFTR mode check")
---
 arch/arm/mach-exynos/common.h    |  2 +-
 arch/arm/mach-exynos/pm.c        | 40 +++++++++++++++++++++++++++++---
 drivers/cpuidle/cpuidle-exynos.c | 10 ++++----
 3 files changed, 44 insertions(+), 8 deletions(-)
arch/arm/mach-exynos/common.h +1 -1
index f4c0d33a29e2..30787e23b066 100644
@@ -144,7 +144,7 @@ extern void exynos_cpu_save_register(void);
 extern void exynos_cpu_restore_register(void);
 extern void exynos_pm_central_suspend(void);
 extern int exynos_pm_central_resume(void);
-extern void exynos_enter_aftr(void);
+extern int exynos_enter_aftr(void);
 #ifdef CONFIG_SMP
 extern void exynos_scu_enable(void);
 #else
arch/arm/mach-exynos/pm.c +37 -3
index 6806af6fcb0b..2cf692003030 100644
@@ -18,6 +18,7 @@
 #include <linux/soc/samsung/exynos-pmu.h>
 
 #include <asm/firmware.h>
+#include <asm/smp_plat.h>
 #include <asm/smp_scu.h>
 #include <asm/suspend.h>
 #include <asm/cacheflush.h>
@@ -151,10 +152,37 @@ static int exynos_aftr_finisher(unsigned long flags)
 	return 1;
 }
 
-void exynos_enter_aftr(void)
+/*
+ * AFTR mode can be entered only when all other cores are powered down.
+ * Checking the cpu online mask is not enough for that: a secondary core is
+ * already running for a while before it is marked online (the boot cpu
+ * waits for it in idle) and it is still running for a while after it has
+ * been marked offline (until it reaches WFI in exynos_cpu_die()).
+ */
+static bool exynos_other_cores_powered(void)
+{
+	unsigned int cpu, this_cpu = smp_processor_id();
+
+	if (!IS_ENABLED(CONFIG_SMP))
+		return false;
+
+	for_each_possible_cpu(cpu) {
+		u32 core_id = MPIDR_AFFINITY_LEVEL(cpu_logical_map(cpu), 0);
+
+		if (cpu != this_cpu && exynos_cpu_power_state(core_id))
+			return true;
+	}
+
+	return false;
+}
+
+int exynos_enter_aftr(void)
 {
 	unsigned int cpuid = smp_processor_id();
 
+	if (exynos_other_cores_powered())
+		return -EBUSY;
+
 	cpu_pm_enter();
 
 	if (soc_is_exynos3250())
@@ -182,6 +210,8 @@ void exynos_enter_aftr(void)
 		exynos_clear_boot_flag(cpuid, C2_STATE);
 
 	cpu_pm_exit();
+
+	return 0;
 }
 
 #if defined(CONFIG_SMP) && defined(CONFIG_ARM_EXYNOS_CPUIDLE)
@@ -227,8 +257,12 @@ static int exynos_cpu0_enter_aftr(void)
 		}
 	}
 
-	exynos_enter_aftr();
-	ret = 0;
+	/*
+	 * CPU1 might be already marked offline, but still not powered down
+	 * (i.e. not yet reached WFI in exynos_cpu_die()), so AFTR has to be
+	 * rejected in such case.
+	 */
+	ret = exynos_enter_aftr();
 
 abort:
 	if (cpu_online(1)) {
drivers/cpuidle/cpuidle-exynos.c +6 -4
index b2b5666e0515..65fb2d9ce952 100644
@@ -22,7 +22,7 @@
 static atomic_t exynos_idle_barrier;
 
 static struct cpuidle_exynos_data *exynos_cpuidle_pdata;
-static void (*exynos_enter_aftr)(void);
+static int (*exynos_enter_aftr)(void);
 
 static int exynos_enter_coupled_lowpower(struct cpuidle_device *dev,
 					 struct cpuidle_driver *drv,
@@ -65,10 +65,12 @@ static int exynos_enter_lowpower(struct cpuidle_device *dev,
 	if (num_online_cpus() > 1 || dev->cpu != 0)
 		new_index = drv->safe_state_index;
 
-	if (new_index == 0)
-		return arm_cpuidle_simple_enter(dev, drv, new_index);
+	/* Fall back to WFI if other cores have not been powered down yet */
+	if (new_index != 0 && !exynos_enter_aftr())
+		return new_index;
 
-	exynos_enter_aftr();
+	new_index = drv->safe_state_index;
+	arm_cpuidle_simple_enter(dev, drv, new_index);
 
 	return new_index;
 }
Signature · 1 line
2.43.0
#5 lore raw

PATCH v2 4/4 cpuidle: exynos: Let AFTR state handle RCU idle itself

In reply to Marek Szyprowski

The cpuidle core calls ct_cpuidle_enter() before invoking the idle state enter() callback, unless the state sets CPUIDLE_FLAG_RCU_IDLE. Exynos AFTR callbacks call cpu_pm_enter()/cpu_pm_exit(), which take a lock and run the CPU PM notifier chain, so they end up being called with RCU not watching. This triggers the following warnings after offlining CPU1:

WARNING: ./include/trace/events/lock.h:23 at lock_acquire+0x2c4/0x3d8, CPU#0: swapper/0/0

RCU not watching for tracepoint
...
lock_acquire from _raw_spin_lock_irqsave+0x4c/0x68 _raw_spin_lock_irqsave from cpu_pm_enter+0xc/0x50 cpu_pm_enter from exynos_enter_aftr+0x18/0x150 exynos_enter_aftr from exynos_enter_lowpower+0x34/0x60 exynos_enter_lowpower from cpuidle_enter_state+0xa8/0x4ec cpuidle_enter_state from cpuidle_enter+0x50/0x54 cpuidle_enter from do_idle+0x164/0x374
...

Fix this by marking the AFTR states (both regular and coupled) with CPUIDLE_FLAG_RCU_IDLE and calling ct_cpuidle_enter()/ct_cpuidle_exit() only around the cpu_suspend() calls, like the big.LITTLE cpuidle driver does. Also handle the WFI fallback in exynos_enter_lowpower(), which now has to switch off RCU on its own.

Fixes 924aed1646bf ("cpuidle, cpu_pm: Remove RCU fiddling from cpu_pm_{enter,exit}()")
---
 arch/arm/mach-exynos/pm.c        |  9 +++++++--
 drivers/cpuidle/cpuidle-exynos.c | 12 ++++++++----
 2 files changed, 15 insertions(+), 6 deletions(-)
arch/arm/mach-exynos/pm.c +7 -2
index 2cf692003030..8c699439dbd3 100644
@@ -12,6 +12,7 @@
 #include <linux/init.h>
 #include <linux/suspend.h>
 #include <linux/cpu_pm.h>
+#include <linux/cpuidle.h>
 #include <linux/io.h>
 #include <linux/of.h>
 #include <linux/soc/samsung/exynos-regs-pmu.h>
@@ -176,7 +177,7 @@ static bool exynos_other_cores_powered(void)
 	return false;
 }
 
-int exynos_enter_aftr(void)
+int __cpuidle exynos_enter_aftr(void)
 {
 	unsigned int cpuid = smp_processor_id();
 
@@ -196,7 +197,9 @@ int exynos_enter_aftr(void)
 			       S5P_CENTRAL_SEQ_OPTION);
 	}
 
+	ct_cpuidle_enter();
 	cpu_suspend(0, exynos_aftr_finisher);
+	ct_cpuidle_exit();
 
 	if (read_cpuid_part() == ARM_CPU_PART_CORTEX_A9) {
 		exynos_scu_enable();
@@ -343,7 +346,7 @@ static int exynos_wfi_finisher(unsigned long flags)
 	return -1;
 }
 
-static int exynos_cpu1_powerdown(void)
+static __cpuidle int exynos_cpu1_powerdown(void)
 {
 	int ret = -1;
 
@@ -361,7 +364,9 @@ static int exynos_cpu1_powerdown(void)
 	if (soc_is_exynos3250())
 		pmu_raw_writel(0, S5P_PMU_SPARE2);
 
+	ct_cpuidle_enter();
 	ret = cpu_suspend(0, exynos_wfi_finisher);
+	ct_cpuidle_exit();
 
 	cpu_pm_exit();
 
drivers/cpuidle/cpuidle-exynos.c +8 -4
index 65fb2d9ce952..d30c569be2ed 100644
@@ -55,9 +55,9 @@ static int exynos_enter_coupled_lowpower(struct cpuidle_device *dev,
 	return index;
 }
 
-static int exynos_enter_lowpower(struct cpuidle_device *dev,
-				struct cpuidle_driver *drv,
-				int index)
+static __cpuidle int exynos_enter_lowpower(struct cpuidle_device *dev,
+					   struct cpuidle_driver *drv,
+					   int index)
 {
 	int new_index = index;
 
@@ -70,7 +70,9 @@ static int exynos_enter_lowpower(struct cpuidle_device *dev,
 		return new_index;
 
 	new_index = drv->safe_state_index;
+	ct_cpuidle_enter();
 	arm_cpuidle_simple_enter(dev, drv, new_index);
+	ct_cpuidle_exit();
 
 	return new_index;
 }
@@ -84,6 +86,7 @@ static struct cpuidle_driver exynos_idle_driver = {
 			.enter			= exynos_enter_lowpower,
 			.exit_latency		= 300,
 			.target_residency	= 10000,
+			.flags			= CPUIDLE_FLAG_RCU_IDLE,
 			.name			= "C1",
 			.desc			= "ARM power down",
 		},
@@ -102,7 +105,8 @@ static struct cpuidle_driver exynos_coupled_idle_driver = {
 			.exit_latency		= 5000,
 			.target_residency	= 10000,
 			.flags			= CPUIDLE_FLAG_COUPLED |
-						  CPUIDLE_FLAG_TIMER_STOP,
+						  CPUIDLE_FLAG_TIMER_STOP |
+						  CPUIDLE_FLAG_RCU_IDLE,
 			.name			= "C1",
 			.desc			= "ARM power down",
 		},
Signature · 1 line
2.43.0