From 3cf44780da51ede970f6ce30b5691fa70fdf9e27 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 18 Nov 2026 16:02:05 +0700 Subject: [PATCH] arm64: spin table: close the holding pen behind each released CPU secondary_holding_pen_release is written with a CPU's MPIDR to let it out of the pen and never reset, so after boot it still names the last CPU released. secondary_data.task likewise keeps that CPU's idle task: __cpu_up() only clears it on failure. With the restartable spin table (aurora,rvbar-spin-table) a CPU comes back through the pen whenever the SoC restarts it at its reset vector: after a system sleep's commit, and at the wake. The CPU whose MPIDR is still in the pen walks straight through into secondary_startup, ahead of its own bring-up, and runs secondary_start_kernel() as that CPU with the stale task. The hotplug state machine has not started it, so no STARTING callback runs (no arch timer, no tick device), yet it marks itself online and completes cpu_running, so the real bring-up returns at once. It idles quietly while cpuidle is paused and faults at the first menu_select() after cpuidle_resume(): Unable to handle kernel NULL pointer dereference at virtual address 0000000000000020 CPU: 9 UID: 0 PID: 0 Comm: swapper/9 pc : tick_nohz_get_sleep_length+0x68/0xd4 Call trace: tick_nohz_get_sleep_length+0x68/0xd4 (P) menu_select+0x414/0x774 cpuidle_select+0x1c/0x28 do_idle+0x1e8/0x310 cpu_startup_entry+0x34/0x38 secondary_start_kernel+0x198/0x1bc Kernel panic - not syncing: Attempted to kill the idle task! Seen on a J815 (MacBook Air 15" M5) when every core was started at the end of the wake, as macOS does, instead of one by one at its bring-up. Reset the release to INVALID_HWID in the released CPU's cpu_postboot, which runs before complete(&cpu_running), so it can't race with the next bring-up; and again before a suspend's hold and after the wake, before any CPU is started. Clear secondary_data.task after a successful bring-up too, as kernels before 5.x did, so a core that still reaches __secondary_switched without a bring-up of its own waits in __secondary_too_slow instead of running as another CPU. Tested on J815: two real deep sleeps (256 s and 172 s) with all nine secondary cores started together at the wake, all back online, where the same kernel without this change panicked on the first wake. Signed-off-by: Nick Co-Authored-By: Claude Opus 5.5 Signed-off-by: Nick --- arch/arm64/kernel/smp.c | 11 ++++++++++- arch/arm64/kernel/smp_spin_table.c | 13 +++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kernel/smp.c b/arch/arm64/kernel/smp.c index f6b5decff53da..f383d15a104cf 100644 --- a/arch/arm64/kernel/smp.c +++ b/arch/arm64/kernel/smp.c @@ -139,8 +139,17 @@ int __cpu_up(unsigned int cpu, struct task_struct *idle) */ wait_for_completion_timeout(&cpu_running, msecs_to_jiffies(5000)); - if (cpu_online(cpu)) + if (cpu_online(cpu)) { + /* + * The CPU has taken its task. Cleared, as kernels before 5.x did, so a core that + * reaches __secondary_switched without a bring-up of its own waits in + * __secondary_too_slow instead of running as this CPU a second time. + */ + WRITE_ONCE(secondary_data.task, NULL); + dcache_clean_inval_poc((unsigned long)&secondary_data, + (unsigned long)&secondary_data + sizeof(secondary_data)); return 0; + } pr_crit("CPU%u: failed to come online\n", cpu); secondary_data.task = NULL; diff --git a/arch/arm64/kernel/smp_spin_table.c b/arch/arm64/kernel/smp_spin_table.c index e66806dbbf9c5..0d0f200e3de6a 100644 --- a/arch/arm64/kernel/smp_spin_table.c +++ b/arch/arm64/kernel/smp_spin_table.c @@ -222,6 +222,9 @@ int spin_table_hold_parked(void) int us, ret; unsigned int cpu; + /* No CPU restarted from here to the wake may pass the pen without its own bring-up. */ + write_pen_release(INVALID_HWID); + for_each_present_cpu(cpu) WRITE_ONCE(spin_table_hold_error[cpu], 0); WRITE_ONCE(spin_table_hold_generation, READ_ONCE(spin_table_hold_generation) + 1); @@ -331,6 +334,8 @@ void spin_table_stop_finished(void) /* Before any CPU is started again: the pen must let them boot. */ if (secondary_pen_deep) spin_table_pen_deep_set(0); + /* ...but only the one its bring-up releases (cores started early wait in the pen). */ + write_pen_release(INVALID_HWID); WRITE_ONCE(spin_table_hold_requested, false); WRITE_ONCE(spin_table_stop_requested, false); WRITE_ONCE(spin_table_stop_released, false); @@ -473,6 +478,14 @@ static int smp_rvbar_spin_table_cpu_kill(unsigned int cpu) static void smp_rvbar_spin_table_postboot(void) { + /* + * Close the pen behind this CPU. Left at its MPIDR, the next restart at the reset vector + * (after a sleep's commit, or a platform that starts every core at once at the wake) + * lets it straight through the pen into secondary_startup ahead of its own bring-up, + * with the secondary_data of its last boot: it then runs as that CPU with no STARTING + * callbacks run, e.g. without a tick device. + */ + write_pen_release(INVALID_HWID); cpumask_clear_cpu(smp_processor_id(), &spin_table_cpus_off); if (READ_ONCE(spin_table_power) && spin_table_power->cpu_restore) spin_table_power->cpu_restore(read_cpuid_mpidr() & MPIDR_HWID_BITMASK);