Skip to contentSkip to conversation

arm64: Allow erratum 1418040 for late CPUs

PATCH Superseded Marc Zyngier 5 participants 10 messages Jul 28, 2020 – Jul 29, 2020 arm64

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover arm64: Allow erratum 1418040 for late CPUs

Erratum 1418040 currently prevents a late CPU from booting if none of the early CPUs are affected by it. This is because the handling is implemented as alternatives, and we have already got rid of them by the time userspace onlines a new CPU.

A solution to this is to move everything into C code, and rely on static keys instead. Once this is done, the feature can be allowed for late CPUs.

Note that CPUs affected by 1418040 also tend to miss AArch32-EL1, meaning they cannot be used as late CPUs when KVM is enabled and that their sibblings have AArch32-EL1.

Marc Zyngier (2):
arm64: Move handling of erratum 1418040 into C code
arm64: Allow booting of late CPUs affected by erratum 1418040

 arch/arm64/kernel/cpu_errata.c |  2 ++
 arch/arm64/kernel/entry.S      | 21 ---------------------
 arch/arm64/kernel/process.c    | 29 +++++++++++++++++++++++++++++
 3 files changed, 31 insertions(+), 21 deletions(-)
Signature · 7 lines
2.27.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#6 lore raw

PATCH 2/2 arm64: Allow booting of late CPUs affected by erratum 1418040

As we can now switch from a system that isn't affected by 1418040 to a system that globally is affected, let's allow affected CPUs to come in at a later time.

---
 arch/arm64/kernel/cpu_errata.c | 2 ++
 1 file changed, 2 insertions(+)
arch/arm64/kernel/cpu_errata.c +2 -0
index 79728bfb5351..2c0b82db825b 100644
@@ -910,6 +910,8 @@ const struct arm64_cpu_capabilities arm64_errata[] = {
 		.desc = "ARM erratum 1418040",
 		.capability = ARM64_WORKAROUND_1418040,
 		ERRATA_MIDR_RANGE_LIST(erratum_1418040_list),
+		.type = (ARM64_CPUCAP_SCOPE_LOCAL_CPU |
+			 ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU),
 	},
 #endif
 #ifdef CONFIG_ARM64_WORKAROUND_SPECULATIVE_AT
Signature · 7 lines
2.27.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#1 lore raw

PATCH 1/2 arm64: Move handling of erratum 1418040 into C code

In reply to Marc Zyngier

Instead of dealing with erratum 1418040 on each entry and exit, let's move the handling to __switch_to() instead, which has several advantages:

- It can be applied when it matters (switching between 32 and 64 bit tasks).
- It is written in C (yay!)
- It can rely on static keys rather than alternatives

---
 arch/arm64/kernel/entry.S   | 21 ---------------------
 arch/arm64/kernel/process.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 21 deletions(-)
arch/arm64/kernel/entry.S +0 -21
index 35de8ba60e3d..44445d471442 100644
@@ -169,19 +169,6 @@ alternative_cb_end
 	stp	x28, x29, [sp, #16 * 14]
 
 	.if	\el == 0
-	.if	\regsize == 32
-	/*
-	 * If we're returning from a 32-bit task on a system affected by
-	 * 1418040 then re-enable userspace access to the virtual counter.
-	 */
-#ifdef CONFIG_ARM64_ERRATUM_1418040
-alternative_if ARM64_WORKAROUND_1418040
-	mrs	x0, cntkctl_el1
-	orr	x0, x0, #2	// ARCH_TIMER_USR_VCT_ACCESS_EN
-	msr	cntkctl_el1, x0
-alternative_else_nop_endif
-#endif
-	.endif
 	clear_gp_regs
 	mrs	x21, sp_el0
 	ldr_this_cpu	tsk, __entry_task, x20
@@ -337,14 +324,6 @@ alternative_else_nop_endif
 	tst	x22, #PSR_MODE32_BIT		// native task?
 	b.eq	3f
 
-#ifdef CONFIG_ARM64_ERRATUM_1418040
-alternative_if ARM64_WORKAROUND_1418040
-	mrs	x0, cntkctl_el1
-	bic	x0, x0, #2			// ARCH_TIMER_USR_VCT_ACCESS_EN
-	msr	cntkctl_el1, x0
-alternative_else_nop_endif
-#endif
-
 #ifdef CONFIG_ARM64_ERRATUM_845719
 alternative_if ARM64_WORKAROUND_845719
 #ifdef CONFIG_PID_IN_CONTEXTIDR
arch/arm64/kernel/process.c +29 -0
index 6089638c7d43..87c33f7c536b 100644
@@ -515,6 +515,34 @@ static void entry_task_switch(struct task_struct *next)
 	__this_cpu_write(__entry_task, next);
 }
 
+static void erratum_1418040_thread_switch(struct task_struct *prev,
+					  struct task_struct *next)
+{
+	bool prev32, next32;
+	u64 val;
+
+	if (!(IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) &&
+	      cpus_have_const_cap(ARM64_WORKAROUND_1418040)))
+		return;
+
+	prev32 = (!(prev->flags & PF_KTHREAD) &&
+		  is_compat_thread(task_thread_info(prev)));
+	next32 = (!(next->flags & PF_KTHREAD) &&
+		  is_compat_thread(task_thread_info(next)));
+
+	if (prev32 == next32)
+		return;
+
+	val = read_sysreg(cntkctl_el1);
+
+	if (prev32 & !next32)
+		val |= ARCH_TIMER_USR_VCT_ACCESS_EN;
+	else
+		val &= ~ARCH_TIMER_USR_VCT_ACCESS_EN;
+
+	write_sysreg(val, cntkctl_el1);
+}
+
 /*
  * Thread switching.
  */
@@ -530,6 +558,7 @@ __notrace_funcgraph struct task_struct *__switch_to(struct task_struct *prev,
 	entry_task_switch(next);
 	uao_thread_switch(next);
 	ssbs_thread_switch(next);
+	erratum_1418040_thread_switch(prev, next);
 
 	/*
 	 * Complete any pending TLB or cache maintenance on this CPU in case
Signature · 7 lines
2.27.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Tested-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] arm64: Move handling of erratum 1418040 into C code

Quoted 9 lines · Marc Zyngier

Instead of dealing with erratum 1418040 on each entry and exit, let's move the handling to __switch_to() instead, which has several advantages:

- It can be applied when it matters (switching between 32 and 64 bit tasks).
- It is written in C (yay!)
- It can rely on static keys rather than alternatives

Quoted 6 lines
---
 arch/arm64/kernel/entry.S   | 21 ---------------------
 arch/arm64/kernel/process.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 21 deletions(-)

Thanks Marc, tested the series on SC7180 and SM8150 SoCs which are affected by this erratum.

Signature · 7 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Tested-by on v1 2/2
#7 lore raw

Re: [PATCH 2/2] arm64: Allow booting of late CPUs affected by erratum 1418040

In reply to Marc Zyngier
Quoted 9 lines · Marc Zyngier

As we can now switch from a system that isn't affected by 1418040 to a system that globally is affected, let's allow affected CPUs to come in at a later time.

---
 arch/arm64/kernel/cpu_errata.c | 2 ++
 1 file changed, 2 insertions(+)

Thanks,
Sai

Signature · 7 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member
of Code Aurora Forum, hosted by The Linux Foundation

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#3 lore raw

Re: [PATCH 1/2] arm64: Move handling of erratum 1418040 into C code

In reply to Marc Zyngier

Quoting Marc Zyngier (2020-07-28 02:21:11)

arch/arm64/kernel/entry.S alternative_cb_end · line 176

Quoted 13 lines
arch/arm64/kernel/entry.S +0 -5
index 35de8ba60e3d..44445d471442 100644
@@ -169,19 +169,6 @@ alternative_cb_end
        stp     x28, x29, [sp, #16 * 14]
 
        .if     \el == 0
-       .if     \regsize == 32
-       /*
-        * If we're returning from a 32-bit task on a system affected by
-        * 1418040 then re-enable userspace access to the virtual counter.
-        */

Can this comment go above the function in C?

Quoted 31 lines
arch/arm64/kernel/entry.S +0 -16
-#ifdef CONFIG_ARM64_ERRATUM_1418040
-alternative_if ARM64_WORKAROUND_1418040
-       mrs     x0, cntkctl_el1
-       orr     x0, x0, #2      // ARCH_TIMER_USR_VCT_ACCESS_EN
-       msr     cntkctl_el1, x0
-alternative_else_nop_endif
-#endif
-       .endif
        clear_gp_regs
        mrs     x21, sp_el0
        ldr_this_cpu    tsk, __entry_task, x20
@@ -337,14 +324,6 @@ alternative_else_nop_endif
        tst     x22, #PSR_MODE32_BIT            // native task?
        b.eq    3f
 
-#ifdef CONFIG_ARM64_ERRATUM_1418040
-alternative_if ARM64_WORKAROUND_1418040
-       mrs     x0, cntkctl_el1
-       bic     x0, x0, #2                      // ARCH_TIMER_USR_VCT_ACCESS_EN
-       msr     cntkctl_el1, x0
-alternative_else_nop_endif
-#endif
-
 #ifdef CONFIG_ARM64_ERRATUM_845719
 alternative_if ARM64_WORKAROUND_845719
 #ifdef CONFIG_PID_IN_CONTEXTIDR
arch/arm64/kernel/process.c +0 -0
index 6089638c7d43..87c33f7c536b 100644

arch/arm64/kernel/process.c static void entry_task_switch(struct task_struct *next) · line 518

Quoted 4 lines
arch/arm64/kernel/process.c +1 -0
@@ -515,6 +515,34 @@ static void entry_task_switch(struct task_struct *next)
        __this_cpu_write(__entry_task, next);
 }
 
+static void erratum_1418040_thread_switch(struct task_struct *prev,

Should it be marked __always_inline so that the cpus_have_const_cap() check can avoid the branch to this function when there's nothing to do?

Quoted 20 lines
arch/arm64/kernel/process.c +20 -0
+                                         struct task_struct *next)
+{
+       bool prev32, next32;
+       u64 val;
+
+       if (!(IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) &&
+             cpus_have_const_cap(ARM64_WORKAROUND_1418040)))
+               return;
+
+       prev32 = (!(prev->flags & PF_KTHREAD) &&
+                 is_compat_thread(task_thread_info(prev)));
+       next32 = (!(next->flags & PF_KTHREAD) &&
+                 is_compat_thread(task_thread_info(next)));
+
+       if (prev32 == next32)
+               return;
+
+       val = read_sysreg(cntkctl_el1);
+
+       if (prev32 & !next32)

arch/arm64/kernel/process.c static void entry_task_switch(struct task_struct *next) · line 548

Quoted 10 lines
arch/arm64/kernel/process.c +7 -0
+               val |= ARCH_TIMER_USR_VCT_ACCESS_EN;
+       else
+               val &= ~ARCH_TIMER_USR_VCT_ACCESS_EN;
+
+       write_sysreg(val, cntkctl_el1);
+}
+
 /*
  * Thread switching.
  */

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

Reviewed-by on v1 2/2
#8 lore raw

Re: [PATCH 2/2] arm64: Allow booting of late CPUs affected by erratum 1418040

In reply to Marc Zyngier

Quoting Marc Zyngier (2020-07-28 02:21:12)

Quoted 6 lines

As we can now switch from a system that isn't affected by 1418040 to a system that globally is affected, let's allow affected CPUs to come in at a later time.

---

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#4 lore raw

Re: [PATCH 1/2] arm64: Move handling of erratum 1418040 into C code

In reply to Stephen Boyd

Hi Stephen,

Quoted 8 lines · Stephen Boyd

Quoting Marc Zyngier (2020-07-28 02:21:11)

Quoted 7 lines
arch/arm64/kernel/entry.S +0 -0
index 35de8ba60e3d..44445d471442 100644
@@ -169,19 +169,6 @@ alternative_cb_end
        stp     x28, x29, [sp, #16 * 14]
 
Quoted 10 lines
Quoted 8 lines
arch/arm64/kernel/entry.S +0 -3
        .if     \el == 0
-       .if     \regsize == 32
-       /*
-        * If we're returning from a 32-bit task on a system affected 

by
- * 1418040 then re-enable userspace access to the virtual
counter.
- */

Can this comment go above the function in C?

It will have to be a slightly different comment, as the logic changes. Something like:

/*
* ARM erratum 1418040 handling affecting the 32bit view of CNTVCT. *
* Assuming the virtual counter is enabled at the beginning of times:
* - disable access when switching from a 64bit task to a 32bit task
* - enable access when switching from a 32bit task to a 64bit task */

Quoted 37 lines
Quoted 37 lines
arch/arm64/kernel/entry.S +0 -12
-#ifdef CONFIG_ARM64_ERRATUM_1418040
-alternative_if ARM64_WORKAROUND_1418040
-       mrs     x0, cntkctl_el1
-       orr     x0, x0, #2      // ARCH_TIMER_USR_VCT_ACCESS_EN
-       msr     cntkctl_el1, x0
-alternative_else_nop_endif
-#endif
-       .endif
        clear_gp_regs
        mrs     x21, sp_el0
        ldr_this_cpu    tsk, __entry_task, x20
@@ -337,14 +324,6 @@ alternative_else_nop_endif
        tst     x22, #PSR_MODE32_BIT            // native task?
        b.eq    3f
 
-#ifdef CONFIG_ARM64_ERRATUM_1418040
-alternative_if ARM64_WORKAROUND_1418040
-       mrs     x0, cntkctl_el1
-       bic     x0, x0, #2                      // 

ARCH_TIMER_USR_VCT_ACCESS_EN

arch/arm64/kernel/entry.S +0 -4
-       msr     cntkctl_el1, x0
-alternative_else_nop_endif
-#endif
-
 #ifdef CONFIG_ARM64_ERRATUM_845719
 alternative_if ARM64_WORKAROUND_845719
 #ifdef CONFIG_PID_IN_CONTEXTIDR
arch/arm64/kernel/process.c +0 -0
index 6089638c7d43..87c33f7c536b 100644
@@ -515,6 +515,34 @@ static void entry_task_switch(struct task_struct 
 *next)
        __this_cpu_write(__entry_task, next);
 }
 
Quoted 4 lines

arch/arm64/kernel/process.c static void entry_task_switch(struct task_struct *next) · line 518

Quoted 1 line
arch/arm64/kernel/process.c +1 -0
+static void erratum_1418040_thread_switch(struct task_struct *prev,

Should it be marked __always_inline so that the cpus_have_const_cap() check can avoid the branch to this function when there's nothing to do?

I'd expect the compiler to directly inline this (in my experience it does, as with most of the other functions called from __switch_to()).

It probably doesn't hurt though, so I'll do that.

Thanks,

        M.
Signature · 6 lines
Jazz is not dead. It just smells funny...

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Reviewed-by on v1 2/2
#9 lore raw

Re: [PATCH 2/2] arm64: Allow booting of late CPUs affected by erratum 1418040

In reply to Marc Zyngier
Quoted 15 lines · Marc Zyngier

As we can now switch from a system that isn't affected by 1418040 to a system that globally is affected, let's allow affected CPUs to come in at a later time.

---
  arch/arm64/kernel/cpu_errata.c | 2 ++
  1 file changed, 2 insertions(+)
arch/arm64/kernel/cpu_errata.c +0 -0
index 79728bfb5351..2c0b82db825b 100644

arch/arm64/kernel/cpu_errata.c const struct arm64_cpu_capabilities arm64_errata[] = { · line 917

Quoted 8 lines
arch/arm64/kernel/cpu_errata.c +2 -0
@@ -910,6 +910,8 @@ const struct arm64_cpu_capabilities arm64_errata[] = {
  		.desc = "ARM erratum 1418040",
  		.capability = ARM64_WORKAROUND_1418040,
  		ERRATA_MIDR_RANGE_LIST(erratum_1418040_list),
+		.type = (ARM64_CPUCAP_SCOPE_LOCAL_CPU |
+			 ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU),
  	},
  #endif
  #ifdef CONFIG_ARM64_WORKAROUND_SPECULATIVE_AT

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

maintainer
#5 lore raw

Re: [PATCH 1/2] arm64: Move handling of erratum 1418040 into C code

In reply to Marc Zyngier
Quoted 12 lines · Marc Zyngier
arch/arm64/kernel/process.c +4 -0
index 6089638c7d43..87c33f7c536b 100644
@@ -515,6 +515,34 @@ static void entry_task_switch(struct task_struct *next)
 	__this_cpu_write(__entry_task, next);
 }
 
+static void erratum_1418040_thread_switch(struct task_struct *prev,
+					  struct task_struct *next)
+{
+	bool prev32, next32;

arch/arm64/kernel/process.c static void entry_task_switch(struct task_struct *next) · line 531

Quoted 10 lines
arch/arm64/kernel/process.c +10 -0
+	u64 val;
+
+	if (!(IS_ENABLED(CONFIG_ARM64_ERRATUM_1418040) &&
+	      cpus_have_const_cap(ARM64_WORKAROUND_1418040)))
+		return;
+
+	prev32 = (!(prev->flags & PF_KTHREAD) &&
+		  is_compat_thread(task_thread_info(prev)));
+	next32 = (!(next->flags & PF_KTHREAD) &&
+		  is_compat_thread(task_thread_info(next)));

I don't think we need to test PF_KTHREAD. is_compat_thread() checks for TIF_32BIT and we never set this on kernel threads (they are cloned from kthreadd).

Signature · 6 lines
Catalin

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel