Skip to content

Commit 25693f1

Browse files
mrutland-armctmarinas
authored andcommitted
arm64: Avoid cpus_have_const_cap() for ARM64_HAS_DIT
In __cpu_suspend_exit() we use cpus_have_const_cap() to check for ARM64_HAS_DIT but this is not necessary and cpus_have_final_cap() of alternative_has_cap_*() would be preferable. For historical reasons, cpus_have_const_cap() is more complicated than it needs to be. Before cpucaps are finalized, it will perform a bitmap test of the system_cpucaps bitmap, and once cpucaps are finalized it will use an alternative branch. This used to be necessary to handle some race conditions in the window between cpucap detection and the subsequent patching of alternatives and static branches, where different branches could be out-of-sync with one another (or w.r.t. alternative sequences). Now that we use alternative branches instead of static branches, these are all patched atomically w.r.t. one another, and there are only a handful of cases that need special care in the window between cpucap detection and alternative patching. Due to the above, it would be nice to remove cpus_have_const_cap(), and migrate callers over to alternative_has_cap_*(), cpus_have_final_cap(), or cpus_have_cap() depending on when their requirements. This will remove redundant instructions and improve code generation, and will make it easier to determine how each callsite will behave before, during, and after alternative patching. The ARM64_HAS_DIT cpucap is detected and patched (along with all other cpucaps) before __cpu_suspend_exit() can run. We'll only use __cpu_suspend_exit() as part of PSCI cpuidle or hibernation, and both of these are intialized after system cpucaps are detected and patched: the PSCI cpuidle driver is registered with a device_initcall, hibernation restoration occurs in a late_initcall, and hibarnation saving is driven by usrspace. Therefore it is not necessary to use cpus_have_const_cap(), and using alternative_has_cap_*() or cpus_have_final_cap() is sufficient. This patch replaces the use of cpus_have_const_cap() with alternative_has_cap_unlikely(), which will avoid generating code to test the system_cpucaps bitmap and should be better for all subsequent calls at runtime. To clearly document the ordering relationship between suspend/resume and alternatives patching, an explicit check for system_capabilities_finalized() is added to cpu_suspend() along with a comment block, which will make it easier to spot issues if code is changed in future to allow these functions to be reached earlier. Signed-off-by: Mark Rutland <[email protected]> Cc: James Morse <[email protected]> Cc: Suzuki K Poulose <[email protected]> Cc: Will Deacon <[email protected]> Signed-off-by: Catalin Marinas <[email protected]>
1 parent 54c8818 commit 25693f1

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

arch/arm64/kernel/suspend.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void notrace __cpu_suspend_exit(void)
6161
* PSTATE was not saved over suspend/resume, re-enable any detected
6262
* features that might not have been set correctly.
6363
*/
64-
if (cpus_have_const_cap(ARM64_HAS_DIT))
64+
if (alternative_has_cap_unlikely(ARM64_HAS_DIT))
6565
set_pstate_dit(1);
6666
__uaccess_enable_hw_pan();
6767

@@ -98,6 +98,15 @@ int cpu_suspend(unsigned long arg, int (*fn)(unsigned long))
9898
struct sleep_stack_data state;
9999
struct arm_cpuidle_irq_context context;
100100

101+
/*
102+
* Some portions of CPU state (e.g. PSTATE.{PAN,DIT}) are initialized
103+
* before alternatives are patched, but are only restored by
104+
* __cpu_suspend_exit() after alternatives are patched. To avoid
105+
* accidentally losing these bits we must not attempt to suspend until
106+
* after alternatives have been patched.
107+
*/
108+
WARN_ON(!system_capabilities_finalized());
109+
101110
/* Report any MTE async fault before going to suspend */
102111
mte_suspend_enter();
103112

0 commit comments

Comments
 (0)