Skip to content

Commit c814ca0

Browse files
author
Ashwin Chaugule
committed
ARM: Check if a CPU has gone offline
PSCIv0.2 adds a new function called AFFINITY_INFO, which can be used to query if a specified CPU has actually gone offline. Calling this function via cpu_kill ensures that a CPU has quiesced after a call to cpu_die. This helps prevent the CPU from doing arbitrary bad things when data or instructions are clobbered (as happens with kexec) in the window between a CPU announcing that it is dead and said CPU leaving the kernel. Signed-off-by: Ashwin Chaugule <ashwin.chaugule@linaro.org> Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: Rob Herring <robh@kernel.org> Acked-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent e1cd3b3 commit c814ca0

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎arch/arm/kernel/psci_smp.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <linux/init.h>
1717
#include <linux/smp.h>
1818
#include <linux/of.h>
19+
#include <linux/delay.h>
20+
#include <uapi/linux/psci.h>
1921

2022
#include <asm/psci.h>
2123
#include <asm/smp_plat.h>
@@ -66,6 +68,36 @@ void __ref psci_cpu_die(unsigned int cpu)
6668
/* We should never return */
6769
panic("psci: cpu %d failed to shutdown\n", cpu);
6870
}
71+
72+
int __ref psci_cpu_kill(unsigned int cpu)
73+
{
74+
int err, i;
75+
76+
if (!psci_ops.affinity_info)
77+
return 1;
78+
/*
79+
* cpu_kill could race with cpu_die and we can
80+
* potentially end up declaring this cpu undead
81+
* while it is dying. So, try again a few times.
82+
*/
83+
84+
for (i = 0; i < 10; i++) {
85+
err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
86+
if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
87+
pr_info("CPU%d killed.\n", cpu);
88+
return 1;
89+
}
90+
91+
msleep(10);
92+
pr_info("Retrying again to check for CPU kill\n");
93+
}
94+
95+
pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
96+
cpu, err);
97+
/* Make platform_cpu_kill() fail. */
98+
return 0;
99+
}
100+
69101
#endif
70102

71103
bool __init psci_smp_available(void)
@@ -78,5 +110,6 @@ struct smp_operations __initdata psci_smp_ops = {
78110
.smp_boot_secondary = psci_boot_secondary,
79111
#ifdef CONFIG_HOTPLUG_CPU
80112
.cpu_die = psci_cpu_die,
113+
.cpu_kill = psci_cpu_kill,
81114
#endif
82115
};

‎arch/arm64/include/asm/cpu_ops.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct device_node;
3939
* from the cpu to be killed.
4040
* @cpu_die: Makes a cpu leave the kernel. Must not fail. Called from the
4141
* cpu being killed.
42+
* @cpu_kill: Ensures a cpu has left the kernel. Called from another cpu.
4243
* @cpu_suspend: Suspends a cpu and saves the required context. May fail owing
4344
* to wrong parameters or error conditions. Called from the
4445
* CPU being suspended. Must be called with IRQs disabled.
@@ -52,6 +53,7 @@ struct cpu_operations {
5253
#ifdef CONFIG_HOTPLUG_CPU
5354
int (*cpu_disable)(unsigned int cpu);
5455
void (*cpu_die)(unsigned int cpu);
56+
int (*cpu_kill)(unsigned int cpu);
5557
#endif
5658
#ifdef CONFIG_ARM64_CPU_SUSPEND
5759
int (*cpu_suspend)(unsigned long);

‎arch/arm64/kernel/psci.c‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/smp.h>
2121
#include <linux/reboot.h>
2222
#include <linux/pm.h>
23+
#include <linux/delay.h>
2324
#include <uapi/linux/psci.h>
2425

2526
#include <asm/compiler.h>
@@ -403,6 +404,35 @@ static void cpu_psci_cpu_die(unsigned int cpu)
403404

404405
pr_crit("unable to power off CPU%u (%d)\n", cpu, ret);
405406
}
407+
408+
static int cpu_psci_cpu_kill(unsigned int cpu)
409+
{
410+
int err, i;
411+
412+
if (!psci_ops.affinity_info)
413+
return 1;
414+
/*
415+
* cpu_kill could race with cpu_die and we can
416+
* potentially end up declaring this cpu undead
417+
* while it is dying. So, try again a few times.
418+
*/
419+
420+
for (i = 0; i < 10; i++) {
421+
err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
422+
if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
423+
pr_info("CPU%d killed.\n", cpu);
424+
return 1;
425+
}
426+
427+
msleep(10);
428+
pr_info("Retrying again to check for CPU kill\n");
429+
}
430+
431+
pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
432+
cpu, err);
433+
/* Make op_cpu_kill() fail. */
434+
return 0;
435+
}
406436
#endif
407437

408438
const struct cpu_operations cpu_psci_ops = {
@@ -413,6 +443,7 @@ const struct cpu_operations cpu_psci_ops = {
413443
#ifdef CONFIG_HOTPLUG_CPU
414444
.cpu_disable = cpu_psci_cpu_disable,
415445
.cpu_die = cpu_psci_cpu_die,
446+
.cpu_kill = cpu_psci_cpu_kill,
416447
#endif
417448
};
418449

‎arch/arm64/kernel/smp.c‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,19 @@ int __cpu_disable(void)
228228
return 0;
229229
}
230230

231+
static int op_cpu_kill(unsigned int cpu)
232+
{
233+
/*
234+
* If we have no means of synchronising with the dying CPU, then assume
235+
* that it is really dead. We can only wait for an arbitrary length of
236+
* time and hope that it's dead, so let's skip the wait and just hope.
237+
*/
238+
if (!cpu_ops[cpu]->cpu_kill)
239+
return 1;
240+
241+
return cpu_ops[cpu]->cpu_kill(cpu);
242+
}
243+
231244
static DECLARE_COMPLETION(cpu_died);
232245

233246
/*
@@ -241,6 +254,15 @@ void __cpu_die(unsigned int cpu)
241254
return;
242255
}
243256
pr_notice("CPU%u: shutdown\n", cpu);
257+
258+
/*
259+
* Now that the dying CPU is beyond the point of no return w.r.t.
260+
* in-kernel synchronisation, try to get the firwmare to help us to
261+
* verify that it has really left the kernel before we consider
262+
* clobbering anything it might still be using.
263+
*/
264+
if (!op_cpu_kill(cpu))
265+
pr_warn("CPU%d may not have shut down cleanly\n", cpu);
244266
}
245267

246268
/*

0 commit comments

Comments
 (0)