* [PATCH v4 0/3] riscv: Per-thread envcfg CSR support
@ 2024-08-14 8:10 Samuel Holland
2024-08-14 8:10 ` [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz Samuel Holland
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Samuel Holland @ 2024-08-14 8:10 UTC (permalink / raw)
To: linux-riscv, Palmer Dabbelt
Cc: Andrew Jones, Conor Dooley, linux-kernel, Deepak Gupta,
Samuel Holland, Albert Ou, Andy Chiu, Charlie Jenkins,
Clément Léger, Conor Dooley, Evan Green, Greentime Hu,
Guo Ren, Leonardo Bras, Paul Walmsley, Sunil V L, Thomas Gleixner,
Xiao Wang, Zhao Ke
This series (or equivalent) is a prerequisite for both user-mode pointer
masking and CFI support, as both of those are per-thread features and
are controlled by fields in the envcfg CSR. These patches are based on
v1 of the pointer masking series[1], with significant input from both
Deepak and Andrew.
[1]: https://lore.kernel.org/linux-riscv/20240319215915.832127-6-samuel.holland@sifive.com/
Changes in v4:
- Rebase on riscv/for-next (v6.11-rc)
- Add Conor's Reviewed-by tags from v2 (missed in v3)
Changes in v3:
- Rebase on riscv/for-next
- Drop use of __initdata due to conflicts with cpufeature.c refactoring
Changes in v2:
- Rebase on riscv/for-next
Samuel Holland (3):
riscv: Enable cbo.zero only when all harts support Zicboz
riscv: Add support for per-thread envcfg CSR values
riscv: Call riscv_user_isa_enable() only on the boot hart
arch/riscv/include/asm/cpufeature.h | 2 +-
arch/riscv/include/asm/processor.h | 1 +
arch/riscv/include/asm/switch_to.h | 8 ++++++++
arch/riscv/kernel/cpufeature.c | 11 ++++++++---
arch/riscv/kernel/smpboot.c | 2 --
arch/riscv/kernel/suspend.c | 4 ++--
6 files changed, 20 insertions(+), 8 deletions(-)
--
2.45.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz
2024-08-14 8:10 [PATCH v4 0/3] riscv: Per-thread envcfg CSR support Samuel Holland
@ 2024-08-14 8:10 ` Samuel Holland
2024-09-12 23:01 ` Charlie Jenkins
2024-08-14 8:10 ` [PATCH v4 2/3] riscv: Add support for per-thread envcfg CSR values Samuel Holland
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Samuel Holland @ 2024-08-14 8:10 UTC (permalink / raw)
To: linux-riscv, Palmer Dabbelt
Cc: Andrew Jones, Conor Dooley, linux-kernel, Deepak Gupta,
Samuel Holland, Conor Dooley, Albert Ou, Andy Chiu,
Charlie Jenkins, Clément Léger, Evan Green,
Paul Walmsley, Sunil V L
Currently, we enable cbo.zero for usermode on each hart that supports
the Zicboz extension. This means that the [ms]envcfg CSR value may
differ between harts. Other features, such as pointer masking and CFI,
require setting [ms]envcfg bits on a per-thread basis. The combination
of these two adds quite some complexity and overhead to context
switching, as we would need to maintain two separate masks for the
per-hart and per-thread bits. Andrew Jones, who originally added Zicboz
support, writes[1][2]:
I've approached Zicboz the same way I would approach all
extensions, which is to be per-hart. I'm not currently aware of
a platform that is / will be composed of harts where some have
Zicboz and others don't, but there's nothing stopping a platform
like that from being built.
So, how about we add code that confirms Zicboz is on all harts.
If any hart does not have it, then we complain loudly and disable
it on all the other harts. If it was just a hardware description
bug, then it'll get fixed. If there's actually a platform which
doesn't have Zicboz on all harts, then, when the issue is reported,
we can decide to not support it, support it with defconfig, or
support it under a Kconfig guard which must be enabled by the user.
Let's follow his suggested solution and require the extension to be
available on all harts, so the envcfg CSR value does not need to change
when a thread migrates between harts. Since we are doing this for all
extensions with fields in envcfg, the CSR itself only needs to be saved/
restored when it is present on all harts.
This should not be a regression as no known hardware has asymmetric
Zicboz support, but if anyone reports seeing the warning, we will
re-evaluate our solution.
Link: https://lore.kernel.org/linux-riscv/20240322-168f191eeb8479b2ea169a5e@orel/ [1]
Link: https://lore.kernel.org/linux-riscv/20240323-28943722feb57a41fb0ff488@orel/ [2]
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Deepak Gupta <debug@rivosinc.com>
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---
(no changes since v3)
Changes in v3:
- Rebase on riscv/for-next
arch/riscv/kernel/cpufeature.c | 7 ++++++-
arch/riscv/kernel/suspend.c | 4 ++--
2 files changed, 8 insertions(+), 3 deletions(-)
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index b427188b28fc..0139d4ea8426 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -28,6 +28,8 @@
#define NUM_ALPHA_EXTS ('z' - 'a' + 1)
+static bool any_cpu_has_zicboz;
+
unsigned long elf_hwcap __read_mostly;
/* Host ISA bitmap */
@@ -98,6 +100,7 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
return -EINVAL;
}
+ any_cpu_has_zicboz = true;
return 0;
}
@@ -918,8 +921,10 @@ unsigned long riscv_get_elf_hwcap(void)
void riscv_user_isa_enable(void)
{
- if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
csr_set(CSR_ENVCFG, ENVCFG_CBZE);
+ else if (any_cpu_has_zicboz)
+ pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
}
#ifdef CONFIG_RISCV_ALTERNATIVE
diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c
index c8cec0cc5833..9a8a0dc035b2 100644
--- a/arch/riscv/kernel/suspend.c
+++ b/arch/riscv/kernel/suspend.c
@@ -14,7 +14,7 @@
void suspend_save_csrs(struct suspend_context *context)
{
- if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
context->envcfg = csr_read(CSR_ENVCFG);
context->tvec = csr_read(CSR_TVEC);
context->ie = csr_read(CSR_IE);
@@ -37,7 +37,7 @@ void suspend_save_csrs(struct suspend_context *context)
void suspend_restore_csrs(struct suspend_context *context)
{
csr_write(CSR_SCRATCH, 0);
- if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
+ if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
csr_write(CSR_ENVCFG, context->envcfg);
csr_write(CSR_TVEC, context->tvec);
csr_write(CSR_IE, context->ie);
--
2.45.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 2/3] riscv: Add support for per-thread envcfg CSR values
2024-08-14 8:10 [PATCH v4 0/3] riscv: Per-thread envcfg CSR support Samuel Holland
2024-08-14 8:10 ` [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz Samuel Holland
@ 2024-08-14 8:10 ` Samuel Holland
2024-09-12 23:01 ` Charlie Jenkins
2024-08-14 8:10 ` [PATCH v4 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart Samuel Holland
2024-10-06 13:29 ` [PATCH v4 0/3] riscv: Per-thread envcfg CSR support patchwork-bot+linux-riscv
3 siblings, 1 reply; 8+ messages in thread
From: Samuel Holland @ 2024-08-14 8:10 UTC (permalink / raw)
To: linux-riscv, Palmer Dabbelt
Cc: Andrew Jones, Conor Dooley, linux-kernel, Deepak Gupta,
Samuel Holland, Albert Ou, Andy Chiu, Charlie Jenkins,
Clément Léger, Evan Green, Greentime Hu, Guo Ren,
Leonardo Bras, Paul Walmsley, Xiao Wang
Some bits in the [ms]envcfg CSR, such as the CFI state and pointer
masking mode, need to be controlled on a per-thread basis. Support this
by keeping a copy of the CSR value in struct thread_struct and writing
it during context switches. It is safe to discard the old CSR value
during the context switch because the CSR is modified only by software,
so the CSR will remain in sync with the copy in thread_struct.
Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to
minimize branchiness in the context switching code.
Since thread_struct is copied during fork(), setting the value for the
init task sets the default value for all other threads.
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Deepak Gupta <debug@rivosinc.com>
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---
(no changes since v1)
arch/riscv/include/asm/processor.h | 1 +
arch/riscv/include/asm/switch_to.h | 8 ++++++++
arch/riscv/kernel/cpufeature.c | 2 +-
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h
index 8702b8721a27..586e4ab701c4 100644
--- a/arch/riscv/include/asm/processor.h
+++ b/arch/riscv/include/asm/processor.h
@@ -124,6 +124,7 @@ struct thread_struct {
unsigned long s[12]; /* s[0]: frame pointer */
struct __riscv_d_ext_state fstate;
unsigned long bad_cause;
+ unsigned long envcfg;
u32 riscv_v_flags;
u32 vstate_ctrl;
struct __riscv_v_ext_state vstate;
diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
index 7594df37cc9f..9685cd85e57c 100644
--- a/arch/riscv/include/asm/switch_to.h
+++ b/arch/riscv/include/asm/switch_to.h
@@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; }
#define __switch_to_fpu(__prev, __next) do { } while (0)
#endif
+static inline void __switch_to_envcfg(struct task_struct *next)
+{
+ asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0",
+ 0, RISCV_ISA_EXT_XLINUXENVCFG, 1)
+ :: "r" (next->thread.envcfg) : "memory");
+}
+
extern struct task_struct *__switch_to(struct task_struct *,
struct task_struct *);
@@ -103,6 +110,7 @@ do { \
__switch_to_vector(__prev, __next); \
if (switch_to_should_flush_icache(__next)) \
local_flush_icache_all(); \
+ __switch_to_envcfg(__next); \
((last) = __switch_to(__prev, __next)); \
} while (0)
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 0139d4ea8426..df3e7e8d6d78 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -922,7 +922,7 @@ unsigned long riscv_get_elf_hwcap(void)
void riscv_user_isa_enable(void)
{
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
- csr_set(CSR_ENVCFG, ENVCFG_CBZE);
+ current->thread.envcfg |= ENVCFG_CBZE;
else if (any_cpu_has_zicboz)
pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
}
--
2.45.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v4 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart
2024-08-14 8:10 [PATCH v4 0/3] riscv: Per-thread envcfg CSR support Samuel Holland
2024-08-14 8:10 ` [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz Samuel Holland
2024-08-14 8:10 ` [PATCH v4 2/3] riscv: Add support for per-thread envcfg CSR values Samuel Holland
@ 2024-08-14 8:10 ` Samuel Holland
2024-09-12 22:59 ` Charlie Jenkins
2024-10-06 13:29 ` [PATCH v4 0/3] riscv: Per-thread envcfg CSR support patchwork-bot+linux-riscv
3 siblings, 1 reply; 8+ messages in thread
From: Samuel Holland @ 2024-08-14 8:10 UTC (permalink / raw)
To: linux-riscv, Palmer Dabbelt
Cc: Andrew Jones, Conor Dooley, linux-kernel, Deepak Gupta,
Samuel Holland, Conor Dooley, Albert Ou, Andy Chiu,
Charlie Jenkins, Clément Léger, Evan Green,
Paul Walmsley, Thomas Gleixner, Xiao Wang, Zhao Ke
Now that the [ms]envcfg CSR value is maintained per thread, not per
hart, riscv_user_isa_enable() only needs to be called once during boot,
to set the value for the init task. This also allows it to be marked as
__init.
Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
Reviewed-by: Deepak Gupta <debug@rivosinc.com>
Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
---
Changes in v4:
- Rebase on riscv/for-next (v6.11-rc)
- Add Conor's Reviewed-by tags from v2 (missed in v3)
Changes in v3:
- Drop use of __initdata due to conflicts with cpufeature.c refactoring
Changes in v2:
- Rebase on riscv/for-next
arch/riscv/include/asm/cpufeature.h | 2 +-
arch/riscv/kernel/cpufeature.c | 4 ++--
arch/riscv/kernel/smpboot.c | 2 --
3 files changed, 3 insertions(+), 5 deletions(-)
diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h
index 45f9c1171a48..ce9a995730c1 100644
--- a/arch/riscv/include/asm/cpufeature.h
+++ b/arch/riscv/include/asm/cpufeature.h
@@ -31,7 +31,7 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
/* Per-cpu ISA extensions. */
extern struct riscv_isainfo hart_isa[NR_CPUS];
-void riscv_user_isa_enable(void);
+void __init riscv_user_isa_enable(void);
#define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \
.name = #_name, \
diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index df3e7e8d6d78..b3b9735cb19a 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -919,12 +919,12 @@ unsigned long riscv_get_elf_hwcap(void)
return hwcap;
}
-void riscv_user_isa_enable(void)
+void __init riscv_user_isa_enable(void)
{
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
current->thread.envcfg |= ENVCFG_CBZE;
else if (any_cpu_has_zicboz)
- pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
+ pr_warn("Zicboz disabled as it is unavailable on some harts\n");
}
#ifdef CONFIG_RISCV_ALTERNATIVE
diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c
index 0f8f1c95ac38..e36d20205bd7 100644
--- a/arch/riscv/kernel/smpboot.c
+++ b/arch/riscv/kernel/smpboot.c
@@ -233,8 +233,6 @@ asmlinkage __visible void smp_callin(void)
numa_add_cpu(curr_cpuid);
set_cpu_online(curr_cpuid, true);
- riscv_user_isa_enable();
-
/*
* Remote cache and TLB flushes are ignored while the CPU is offline,
* so flush them both right now just in case.
--
2.45.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v4 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart
2024-08-14 8:10 ` [PATCH v4 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart Samuel Holland
@ 2024-09-12 22:59 ` Charlie Jenkins
0 siblings, 0 replies; 8+ messages in thread
From: Charlie Jenkins @ 2024-09-12 22:59 UTC (permalink / raw)
To: Samuel Holland
Cc: linux-riscv, Palmer Dabbelt, Andrew Jones, Conor Dooley,
linux-kernel, Deepak Gupta, Conor Dooley, Albert Ou, Andy Chiu,
Clément Léger, Evan Green, Paul Walmsley,
Thomas Gleixner, Xiao Wang, Zhao Ke
On Wed, Aug 14, 2024 at 01:10:56AM -0700, Samuel Holland wrote:
> Now that the [ms]envcfg CSR value is maintained per thread, not per
> hart, riscv_user_isa_enable() only needs to be called once during boot,
> to set the value for the init task. This also allows it to be marked as
> __init.
>
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
> Reviewed-by: Deepak Gupta <debug@rivosinc.com>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
> ---
>
> Changes in v4:
> - Rebase on riscv/for-next (v6.11-rc)
> - Add Conor's Reviewed-by tags from v2 (missed in v3)
>
> Changes in v3:
> - Drop use of __initdata due to conflicts with cpufeature.c refactoring
>
> Changes in v2:
> - Rebase on riscv/for-next
>
> arch/riscv/include/asm/cpufeature.h | 2 +-
> arch/riscv/kernel/cpufeature.c | 4 ++--
> arch/riscv/kernel/smpboot.c | 2 --
> 3 files changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/arch/riscv/include/asm/cpufeature.h b/arch/riscv/include/asm/cpufeature.h
> index 45f9c1171a48..ce9a995730c1 100644
> --- a/arch/riscv/include/asm/cpufeature.h
> +++ b/arch/riscv/include/asm/cpufeature.h
> @@ -31,7 +31,7 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
> /* Per-cpu ISA extensions. */
> extern struct riscv_isainfo hart_isa[NR_CPUS];
>
> -void riscv_user_isa_enable(void);
> +void __init riscv_user_isa_enable(void);
>
> #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \
> .name = #_name, \
> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index df3e7e8d6d78..b3b9735cb19a 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -919,12 +919,12 @@ unsigned long riscv_get_elf_hwcap(void)
> return hwcap;
> }
>
> -void riscv_user_isa_enable(void)
> +void __init riscv_user_isa_enable(void)
> {
> if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
> current->thread.envcfg |= ENVCFG_CBZE;
> else if (any_cpu_has_zicboz)
> - pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
> + pr_warn("Zicboz disabled as it is unavailable on some harts\n");
> }
>
> #ifdef CONFIG_RISCV_ALTERNATIVE
> diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c
> index 0f8f1c95ac38..e36d20205bd7 100644
> --- a/arch/riscv/kernel/smpboot.c
> +++ b/arch/riscv/kernel/smpboot.c
> @@ -233,8 +233,6 @@ asmlinkage __visible void smp_callin(void)
> numa_add_cpu(curr_cpuid);
> set_cpu_online(curr_cpuid, true);
>
> - riscv_user_isa_enable();
> -
> /*
> * Remote cache and TLB flushes are ignored while the CPU is offline,
> * so flush them both right now just in case.
> --
> 2.45.1
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz
2024-08-14 8:10 ` [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz Samuel Holland
@ 2024-09-12 23:01 ` Charlie Jenkins
0 siblings, 0 replies; 8+ messages in thread
From: Charlie Jenkins @ 2024-09-12 23:01 UTC (permalink / raw)
To: Samuel Holland
Cc: linux-riscv, Palmer Dabbelt, Andrew Jones, Conor Dooley,
linux-kernel, Deepak Gupta, Conor Dooley, Albert Ou, Andy Chiu,
Clément Léger, Evan Green, Paul Walmsley, Sunil V L
On Wed, Aug 14, 2024 at 01:10:54AM -0700, Samuel Holland wrote:
> Currently, we enable cbo.zero for usermode on each hart that supports
> the Zicboz extension. This means that the [ms]envcfg CSR value may
> differ between harts. Other features, such as pointer masking and CFI,
> require setting [ms]envcfg bits on a per-thread basis. The combination
> of these two adds quite some complexity and overhead to context
> switching, as we would need to maintain two separate masks for the
> per-hart and per-thread bits. Andrew Jones, who originally added Zicboz
> support, writes[1][2]:
>
> I've approached Zicboz the same way I would approach all
> extensions, which is to be per-hart. I'm not currently aware of
> a platform that is / will be composed of harts where some have
> Zicboz and others don't, but there's nothing stopping a platform
> like that from being built.
>
> So, how about we add code that confirms Zicboz is on all harts.
> If any hart does not have it, then we complain loudly and disable
> it on all the other harts. If it was just a hardware description
> bug, then it'll get fixed. If there's actually a platform which
> doesn't have Zicboz on all harts, then, when the issue is reported,
> we can decide to not support it, support it with defconfig, or
> support it under a Kconfig guard which must be enabled by the user.
>
> Let's follow his suggested solution and require the extension to be
> available on all harts, so the envcfg CSR value does not need to change
> when a thread migrates between harts. Since we are doing this for all
> extensions with fields in envcfg, the CSR itself only needs to be saved/
> restored when it is present on all harts.
>
> This should not be a regression as no known hardware has asymmetric
> Zicboz support, but if anyone reports seeing the warning, we will
> re-evaluate our solution.
>
> Link: https://lore.kernel.org/linux-riscv/20240322-168f191eeb8479b2ea169a5e@orel/ [1]
> Link: https://lore.kernel.org/linux-riscv/20240323-28943722feb57a41fb0ff488@orel/ [2]
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> Reviewed-by: Conor Dooley <conor.dooley@microchip.com>
> Reviewed-by: Deepak Gupta <debug@rivosinc.com>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> ---
I realized I was looking at v4 but responding to v3, whoops. I will put
my tags here as well.
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
Tested-by: Charlie Jenkins <charlie@rivosinc.com>
>
> (no changes since v3)
>
> Changes in v3:
> - Rebase on riscv/for-next
>
> arch/riscv/kernel/cpufeature.c | 7 ++++++-
> arch/riscv/kernel/suspend.c | 4 ++--
> 2 files changed, 8 insertions(+), 3 deletions(-)
>
> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index b427188b28fc..0139d4ea8426 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -28,6 +28,8 @@
>
> #define NUM_ALPHA_EXTS ('z' - 'a' + 1)
>
> +static bool any_cpu_has_zicboz;
> +
> unsigned long elf_hwcap __read_mostly;
>
> /* Host ISA bitmap */
> @@ -98,6 +100,7 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
> pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
> return -EINVAL;
> }
> + any_cpu_has_zicboz = true;
> return 0;
> }
>
> @@ -918,8 +921,10 @@ unsigned long riscv_get_elf_hwcap(void)
>
> void riscv_user_isa_enable(void)
> {
> - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
> + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
> csr_set(CSR_ENVCFG, ENVCFG_CBZE);
> + else if (any_cpu_has_zicboz)
> + pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
> }
>
> #ifdef CONFIG_RISCV_ALTERNATIVE
> diff --git a/arch/riscv/kernel/suspend.c b/arch/riscv/kernel/suspend.c
> index c8cec0cc5833..9a8a0dc035b2 100644
> --- a/arch/riscv/kernel/suspend.c
> +++ b/arch/riscv/kernel/suspend.c
> @@ -14,7 +14,7 @@
>
> void suspend_save_csrs(struct suspend_context *context)
> {
> - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
> + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
> context->envcfg = csr_read(CSR_ENVCFG);
> context->tvec = csr_read(CSR_TVEC);
> context->ie = csr_read(CSR_IE);
> @@ -37,7 +37,7 @@ void suspend_save_csrs(struct suspend_context *context)
> void suspend_restore_csrs(struct suspend_context *context)
> {
> csr_write(CSR_SCRATCH, 0);
> - if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
> + if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
> csr_write(CSR_ENVCFG, context->envcfg);
> csr_write(CSR_TVEC, context->tvec);
> csr_write(CSR_IE, context->ie);
> --
> 2.45.1
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 2/3] riscv: Add support for per-thread envcfg CSR values
2024-08-14 8:10 ` [PATCH v4 2/3] riscv: Add support for per-thread envcfg CSR values Samuel Holland
@ 2024-09-12 23:01 ` Charlie Jenkins
0 siblings, 0 replies; 8+ messages in thread
From: Charlie Jenkins @ 2024-09-12 23:01 UTC (permalink / raw)
To: Samuel Holland
Cc: linux-riscv, Palmer Dabbelt, Andrew Jones, Conor Dooley,
linux-kernel, Deepak Gupta, Albert Ou, Andy Chiu,
Clément Léger, Evan Green, Greentime Hu, Guo Ren,
Leonardo Bras, Paul Walmsley, Xiao Wang
On Wed, Aug 14, 2024 at 01:10:55AM -0700, Samuel Holland wrote:
> Some bits in the [ms]envcfg CSR, such as the CFI state and pointer
> masking mode, need to be controlled on a per-thread basis. Support this
> by keeping a copy of the CSR value in struct thread_struct and writing
> it during context switches. It is safe to discard the old CSR value
> during the context switch because the CSR is modified only by software,
> so the CSR will remain in sync with the copy in thread_struct.
>
> Use ALTERNATIVE directly instead of riscv_has_extension_unlikely() to
> minimize branchiness in the context switching code.
>
> Since thread_struct is copied during fork(), setting the value for the
> init task sets the default value for all other threads.
>
> Reviewed-by: Andrew Jones <ajones@ventanamicro.com>
> Reviewed-by: Deepak Gupta <debug@rivosinc.com>
> Signed-off-by: Samuel Holland <samuel.holland@sifive.com>
> ---
Reviewed-by: Charlie Jenkins <charlie@rivosinc.com>
>
> (no changes since v1)
>
> arch/riscv/include/asm/processor.h | 1 +
> arch/riscv/include/asm/switch_to.h | 8 ++++++++
> arch/riscv/kernel/cpufeature.c | 2 +-
> 3 files changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h
> index 8702b8721a27..586e4ab701c4 100644
> --- a/arch/riscv/include/asm/processor.h
> +++ b/arch/riscv/include/asm/processor.h
> @@ -124,6 +124,7 @@ struct thread_struct {
> unsigned long s[12]; /* s[0]: frame pointer */
> struct __riscv_d_ext_state fstate;
> unsigned long bad_cause;
> + unsigned long envcfg;
> u32 riscv_v_flags;
> u32 vstate_ctrl;
> struct __riscv_v_ext_state vstate;
> diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h
> index 7594df37cc9f..9685cd85e57c 100644
> --- a/arch/riscv/include/asm/switch_to.h
> +++ b/arch/riscv/include/asm/switch_to.h
> @@ -70,6 +70,13 @@ static __always_inline bool has_fpu(void) { return false; }
> #define __switch_to_fpu(__prev, __next) do { } while (0)
> #endif
>
> +static inline void __switch_to_envcfg(struct task_struct *next)
> +{
> + asm volatile (ALTERNATIVE("nop", "csrw " __stringify(CSR_ENVCFG) ", %0",
> + 0, RISCV_ISA_EXT_XLINUXENVCFG, 1)
> + :: "r" (next->thread.envcfg) : "memory");
> +}
> +
> extern struct task_struct *__switch_to(struct task_struct *,
> struct task_struct *);
>
> @@ -103,6 +110,7 @@ do { \
> __switch_to_vector(__prev, __next); \
> if (switch_to_should_flush_icache(__next)) \
> local_flush_icache_all(); \
> + __switch_to_envcfg(__next); \
> ((last) = __switch_to(__prev, __next)); \
> } while (0)
>
> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index 0139d4ea8426..df3e7e8d6d78 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -922,7 +922,7 @@ unsigned long riscv_get_elf_hwcap(void)
> void riscv_user_isa_enable(void)
> {
> if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
> - csr_set(CSR_ENVCFG, ENVCFG_CBZE);
> + current->thread.envcfg |= ENVCFG_CBZE;
> else if (any_cpu_has_zicboz)
> pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
> }
> --
> 2.45.1
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v4 0/3] riscv: Per-thread envcfg CSR support
2024-08-14 8:10 [PATCH v4 0/3] riscv: Per-thread envcfg CSR support Samuel Holland
` (2 preceding siblings ...)
2024-08-14 8:10 ` [PATCH v4 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart Samuel Holland
@ 2024-10-06 13:29 ` patchwork-bot+linux-riscv
3 siblings, 0 replies; 8+ messages in thread
From: patchwork-bot+linux-riscv @ 2024-10-06 13:29 UTC (permalink / raw)
To: Samuel Holland
Cc: linux-riscv, palmer, leobras, aou, xiao.w.wang, paul.walmsley,
charlie, linux-kernel, conor, conor.dooley, evan, guoren,
andy.chiu, greentime.hu, cleger, tglx, ajones, ke.zhao, debug
Hello:
This series was applied to riscv/linux.git (for-next)
by Palmer Dabbelt <palmer@rivosinc.com>:
On Wed, 14 Aug 2024 01:10:53 -0700 you wrote:
> This series (or equivalent) is a prerequisite for both user-mode pointer
> masking and CFI support, as both of those are per-thread features and
> are controlled by fields in the envcfg CSR. These patches are based on
> v1 of the pointer masking series[1], with significant input from both
> Deepak and Andrew.
>
> [1]: https://lore.kernel.org/linux-riscv/20240319215915.832127-6-samuel.holland@sifive.com/
>
> [...]
Here is the summary with links:
- [v4,1/3] riscv: Enable cbo.zero only when all harts support Zicboz
https://git.kernel.org/riscv/c/1b57747e978f
- [v4,2/3] riscv: Add support for per-thread envcfg CSR values
https://git.kernel.org/riscv/c/5fc7355f0137
- [v4,3/3] riscv: Call riscv_user_isa_enable() only on the boot hart
https://git.kernel.org/riscv/c/368546ebe7e7
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2024-10-06 13:29 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-14 8:10 [PATCH v4 0/3] riscv: Per-thread envcfg CSR support Samuel Holland
2024-08-14 8:10 ` [PATCH v4 1/3] riscv: Enable cbo.zero only when all harts support Zicboz Samuel Holland
2024-09-12 23:01 ` Charlie Jenkins
2024-08-14 8:10 ` [PATCH v4 2/3] riscv: Add support for per-thread envcfg CSR values Samuel Holland
2024-09-12 23:01 ` Charlie Jenkins
2024-08-14 8:10 ` [PATCH v4 3/3] riscv: Call riscv_user_isa_enable() only on the boot hart Samuel Holland
2024-09-12 22:59 ` Charlie Jenkins
2024-10-06 13:29 ` [PATCH v4 0/3] riscv: Per-thread envcfg CSR support patchwork-bot+linux-riscv
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox