From c5daf572c69cf596d254050089476e9793f67e53 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 18 Nov 2026 16:02:05 +0700 Subject: [PATCH 1/2] 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 --- 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); From 8be9e731e3137c498a7b7dc5b0f09cce3fcf4a2c Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 18 Nov 2026 16:03:40 +0700 Subject: [PATCH 2/2] soc: apple: sleep: T8142: start the cores and write the cluster words as XNU A traced macOS 27.2 sleep on a J813 (boot, a 40 s sleep, the wake and three more entries) differs from ours in three places: - The wake: 16.5 ms after it, right after the memory side and the WD2 stop, the boot core starts all nine other cores in one 0.23 ms burst, three words each (+4 the core's bit, +8 the E core's bit or 0, +0xc the P core's bit or 0). We started each core later, from its own bring-up, with two words. - The commit: the clusters' words are read-modify-written. The first sleep after boot reads 0xf and writes 0; every commit after a wake reads 0x2f and writes 0x20, keeping bit 5. We wrote 0 every time. - The SoC/DCS perf-state page is never accessed. Do the same: - At the end of the wake, start the cores that were online when the sleep began, in one burst with those three words. They come up through the boot object into the kernel's holding pen and wait there; each bring-up only releases its core. The per-core start stays for runtime hotplug. T8142's data has no cpu_clusters, so the cluster comes from the MPIDR. - Clear only the clusters' on bits at the commit. - Drop the SoC/DCS perf-state step (t8142_perf_down()) and the apple_sleep.perf_down switch that chose it. The burst depends on the holding pen being closed behind each released CPU ("arm64: spin table: close the holding pen behind each released CPU"); without it the last CPU booted runs ahead of its bring-up. Tested on J815 (MacBook Air 15" M5): two real deep sleeps (256 s and 172 s), each with "9 cores started in one burst" and nine "Booted secondary processor", all CPUs back, display, NVMe, Wi-Fi and Bluetooth up; the read-modify-write and no perf-state step in a third sleep of 366 s before that. On two J813s (sithglan, gig3m) alarm and lid wakes with the same behaviour. Reverting this commit is how to go back. Signed-off-by: Nick --- drivers/soc/apple/sleep.c | 111 ++++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 53 deletions(-) diff --git a/drivers/soc/apple/sleep.c b/drivers/soc/apple/sleep.c index 9b493cd1575b3..4faf0d4630903 100644 --- a/drivers/soc/apple/sleep.c +++ b/drivers/soc/apple/sleep.c @@ -816,6 +816,7 @@ struct apple_sleep { void __iomem *ps; void __iomem *pwrgate; void __iomem *cpu_start; + unsigned int cpu_burst_n; /* cores the last wake's burst started */ void __iomem *cllt; void __iomem *cluster; void __iomem *commit; @@ -1060,9 +1061,6 @@ struct apple_sleep { void __iomem *acc_e; void __iomem *acc_p; int cluster_p_ret; - int perf_soc_ret; - int perf_dcs_ret; - unsigned int perf_dcs_from; /* With radio_retrain: the radio's PCIe host, and its port's blocks and tables */ struct device_node *radio_host_np; void __iomem *radio_port; @@ -2252,9 +2250,17 @@ MODULE_PARM_DESC(mem_restore, "T8142: put the memory side (AMCC, SoC NI, DCS) ba static bool sleep_clk_restore = true; module_param_named(clk_restore, sleep_clk_restore, bool, 0644); MODULE_PARM_DESC(clk_restore, "T8142: put the 126 PMGR clkgen slots and the memory clock word back at the wake as XNU does"); -static bool sleep_perf_down = true; -module_param_named(perf_down, sleep_perf_down, bool, 0644); -MODULE_PARM_DESC(perf_down, "T8142: SoC/DCS perf states down before the memory stop (macOS on J813 never writes them; A/B)"); +/* + * The cores' start after a T8142 wake. A traced macOS 27.2 run on J813 (boot, a 40 s sleep, the + * wake and three more entries) starts all nine other cores 16.5 ms after the wake, right after + * the memory side and the WD2 stop, in one 0.23 ms burst, three words each: +4 the core's bit, +8 + * the E core's bit or 0, +0xc the P core's bit or 0. The wake does the same for the cores that + * were online when the sleep began (t8142_cpu_burst()); they come up through the boot object + * into the kernel's holding pen and wait there, and each bring-up only releases its core. + */ +static struct cpumask sleep_cpus_burst; +/* The CPUs online at .begin: the ones the PM core brings back after the wake. */ +static struct cpumask sleep_cpus_awake; static u64 t8142_boot_mpidr = ~0ULL; extern char t8142_el1_wfi_stub[], t8142_el1_wfi_stub_end[]; @@ -2514,6 +2520,13 @@ static int sleep_cpu_on(u64 mpidr) bool trace; DEFINE_DYNAMIC_DEBUG_METADATA(cpu_on_trace, "apple-sleep: cpu start after a sleep\n"); + /* Started by the wake's burst: it waits in the pen; release it. */ + if (cpu >= 0 && cpumask_test_and_clear_cpu(cpu, &sleep_cpus_burst)) { + pr_debug("apple-sleep: cpu%d start %#llx: started in the wake burst (%u cores), released from the pen\n", + cpu, mpidr, s->cpu_burst_n); + return 0; + } + trace = DYNAMIC_DEBUG_BRANCH(cpu_on_trace) && s->soc->cpu_clusters >= 2; if (trace) { slot = sleep_cpu_slot_map(cpu); @@ -6372,6 +6385,8 @@ static int apple_sleep_begin(suspend_state_t state) /* The PM core also calls .begin for s2idle when no s2idle ops are installed. */ if (state != PM_SUSPEND_MEM) return 0; + /* Before the PM core takes the other CPUs down: the wake's burst starts only these. */ + cpumask_copy(&sleep_cpus_awake, cpu_online_mask); ret = apple_sleep_profile_check(apple_sleep); if (ret) @@ -6817,9 +6832,6 @@ static void apple_sleep_report(void) s->soc->quiesce.offset[i]); if (s->cpus_stop_ret) dev_err(s->dev, "other CPUs did not stop: %d\n", s->cpus_stop_ret); - if (s->perf_soc_ret || s->perf_dcs_ret) - dev_warn(s->dev, "sleep perf states: SoC %d, DCS %d (DCS was %u)\n", s->perf_soc_ret, - s->perf_dcs_ret, s->perf_dcs_from); if (s->cluster_p_ret) dev_err(s->dev, "P complex did not come back on: %d\n", s->cluster_p_ret); if (s->radio_trainable && s->radio_stop_ret) @@ -8292,15 +8304,6 @@ static const struct apple_sleep_soc t8132_sleep_soc = { */ #define T8142_PMC_VOTER_AP (3 * PMC_VOTER_STRIDE) #define T8142_CLUSTER_BUSY BIT(4) -/* - * AppleT8142PMGR::setPerfState's words in PMGRCLKGEN_SOC_TOP (/arm-io/pmgr reg[2]): the SoC's - * at 0x24000, DCS's at 0x25000, each like T8132's: [3:0] asks, [11:8] has reached, bit 31 busy. - */ -#define T8142_PERF_REQ GENMASK(3, 0) -#define T8142_DCS_SLEEP_STATE 3 -#define T8142_DCS_STEP_STATE 5 -#define T8142_DCS_STEP_FROM 6 -#define T8142_DCS_MAX_STATE 10 /* * Every Linux access to the complexes' ACC word 0xe440f8 has hung J815 so far: a read at @@ -8313,32 +8316,6 @@ module_param_named(acc_wake, sleep_acc_wake, bool, 0644); MODULE_PARM_DESC(acc_wake, "T8142: write ACC 0xe440f8 = 1 per complex at a wake, as restoreHW does (default 0)"); -static int t8142_perf_request(void __iomem *reg, unsigned int state) -{ - writel((readl(reg) & ~T8142_PERF_REQ) | FIELD_PREP(T8142_PERF_REQ, state), reg); - return sleep_perf_wait(reg, state); -} - -/* - * AppleT8142PMGR::quiescePerfState, the PMGR's first sleep step: the SoC to Vmin (3), then DCS - * to 3, through 5 from 6..10. macOS waits on BUSY with no limit and panics on a DCS state - * above 10; here each wait is bounded, and such a state is left alone. - */ -static void t8142_perf_down(struct apple_sleep *s) -{ - unsigned int state = FIELD_GET(DCS_STATE, readl(s->dcs)); - - s->perf_dcs_from = state; - s->perf_soc_ret = t8142_perf_request(s->clk_perf + CLK_PERF_SOC, SOC_SLEEP_STATE); - if (state > T8142_DCS_MAX_STATE) { - s->perf_dcs_ret = -ERANGE; - return; - } - if (state >= T8142_DCS_STEP_FROM) - t8142_perf_request(s->dcs, T8142_DCS_STEP_STATE); - s->perf_dcs_ret = t8142_perf_request(s->dcs, T8142_DCS_SLEEP_STATE); -} - /* AppleT8142PMGR vtable 0xfa0: each complex's CPM gate word, read-modify-write. */ static void t8142_cpm_on(struct apple_sleep *s) { @@ -8643,10 +8620,7 @@ static void t8142_commit_note(struct apple_sleep *s) return; if (s->cpus_stop_ret) flags |= 0x01; - if (s->perf_soc_ret) - flags |= 0x02; - if (s->perf_dcs_ret) - flags |= 0x04; + /* 0x02 and 0x04 were the SoC and DCS perf-state steps, which are gone. */ for (i = 0; i < s->soc->wake_words; i++) { u32 pend = readl(s->wake + s->soc->wake_status + i * 4) & s->wake_armed[i]; @@ -8989,9 +8963,7 @@ static void t8142_last_mile(struct apple_sleep *s) const struct apple_sleep_soc *soc = s->soc; int i; - /* Not in macOS's J813 entry (its perf-state page is untouched in the whole trace): A/B. */ - if (READ_ONCE(sleep_perf_down)) - t8142_perf_down(s); + /* The SoC and DCS perf states stay: macOS's J813 entry never writes them. */ sleep_note(s, 0x64); /* T6050's commit window: the floor's voter to 0, not read, right before the memory stop */ @@ -9010,8 +8982,11 @@ static void t8142_last_mile(struct apple_sleep *s) t8142_commit_note(s); - for (i = 0; i < soc->ncluster_cpu; i++) - writel(0, s->cluster + soc->cluster_cpu[i]); + for (i = 0; i < soc->ncluster_cpu; i++) { + void __iomem *reg = s->cluster + soc->cluster_cpu[i]; + + writel(readl(reg) & ~CLUSTER_ON, reg); + } s->quiesce_still_on = 0; for (i = 0; i < soc->quiesce.count; i++) @@ -9133,6 +9108,35 @@ static int t8142_cluster_on(struct apple_sleep *s, u32 word, void __iomem *acc) return ret; } +/* The other cores started at once, three words each (see sleep_cpus_burst). */ +static void t8142_cpu_burst(struct apple_sleep *s) +{ + unsigned int cpu, n = 0, self = smp_processor_id(); + + cpumask_clear(&sleep_cpus_burst); + s->cpu_burst_n = 0; + /* T8142's data states no cpu_clusters (0): E = MPIDR cluster 0, P = 1, from the MPIDR. */ + if (!s->cpu_start) + return; + for_each_cpu(cpu, &sleep_cpus_awake) { + u64 mpidr = cpu_logical_map(cpu); + unsigned int cluster = sleep_cpu_cluster(s, mpidr), core = sleep_cpu_core(mpidr); + + if (cpu == self || cluster > 1) + continue; + writel(sleep_cpu_start_bit(s, mpidr), s->cpu_start + CPU_START_SYS); + writel(cluster == 0 ? BIT(core) : 0, s->cpu_start + CPU_START_CLUSTER(0)); + writel(cluster == 1 ? BIT(core) : 0, s->cpu_start + CPU_START_CLUSTER(1)); + cpumask_set_cpu(cpu, &sleep_cpus_burst); + n++; + } + s->cpu_burst_n = n; + pr_info("apple-sleep: wake: %u cores started in one burst; stop/sys/e/p now %x/%x/%x/%x\n", + n, readl(s->cpu_start + CPU_START_STOP), readl(s->cpu_start + CPU_START_SYS), + readl(s->cpu_start + CPU_START_CLUSTER(0)), + readl(s->cpu_start + CPU_START_CLUSTER(1))); +} + /* * AppleT8142PMGR::restoreHW: each complex on, pwrgate +0x0 and +0x60 read-modify-written, the * words back, the CPM gates, the ACC words once more. Its two "early wake hacks" (SEP's ps @@ -9193,6 +9197,7 @@ static void t8142_wake(struct apple_sleep *s) s->aon_wdt_marked = false; } apple_sleep_set_wd2(s, WDT_SUSPEND_SECS); + t8142_cpu_burst(s); if (s->pcie_ans_stopped) s->pcie_ans_on_ret = apple_sleep_raise_apcie(s);