Skip to content

Commit 1b57747

Browse files
SiFiveHollandpalmer-dabbelt
authored andcommitted
riscv: Enable cbo.zero only when all harts support Zicboz
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 <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Reviewed-by: Deepak Gupta <[email protected]> Signed-off-by: Samuel Holland <[email protected]> Reviewed-by: Charlie Jenkins <[email protected]> Tested-by: Charlie Jenkins <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 9852d85 commit 1b57747

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

arch/riscv/kernel/cpufeature.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
#define NUM_ALPHA_EXTS ('z' - 'a' + 1)
3030

31+
static bool any_cpu_has_zicboz;
32+
3133
unsigned long elf_hwcap __read_mostly;
3234

3335
/* Host ISA bitmap */
@@ -98,6 +100,7 @@ static int riscv_ext_zicboz_validate(const struct riscv_isa_ext_data *data,
98100
pr_err("Zicboz disabled as cboz-block-size present, but is not a power-of-2\n");
99101
return -EINVAL;
100102
}
103+
any_cpu_has_zicboz = true;
101104
return 0;
102105
}
103106

@@ -919,8 +922,10 @@ unsigned long riscv_get_elf_hwcap(void)
919922

920923
void riscv_user_isa_enable(void)
921924
{
922-
if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_ZICBOZ))
925+
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICBOZ))
923926
csr_set(CSR_ENVCFG, ENVCFG_CBZE);
927+
else if (any_cpu_has_zicboz)
928+
pr_warn_once("Zicboz disabled as it is unavailable on some harts\n");
924929
}
925930

926931
#ifdef CONFIG_RISCV_ALTERNATIVE

arch/riscv/kernel/suspend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
void suspend_save_csrs(struct suspend_context *context)
1616
{
17-
if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
17+
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
1818
context->envcfg = csr_read(CSR_ENVCFG);
1919
context->tvec = csr_read(CSR_TVEC);
2020
context->ie = csr_read(CSR_IE);
@@ -37,7 +37,7 @@ void suspend_save_csrs(struct suspend_context *context)
3737
void suspend_restore_csrs(struct suspend_context *context)
3838
{
3939
csr_write(CSR_SCRATCH, 0);
40-
if (riscv_cpu_has_extension_unlikely(smp_processor_id(), RISCV_ISA_EXT_XLINUXENVCFG))
40+
if (riscv_has_extension_unlikely(RISCV_ISA_EXT_XLINUXENVCFG))
4141
csr_write(CSR_ENVCFG, context->envcfg);
4242
csr_write(CSR_TVEC, context->tvec);
4343
csr_write(CSR_IE, context->ie);

0 commit comments

Comments
 (0)