diff mbox series

[1/3] RISC-V: Improve use of isa2hwcap[]

Message ID 20221021105905.206385-2-ajones@ventanamicro.com (mailing list archive)
State Superseded
Headers show
Series RISC-V: Ensure Zicbom has a valid block size | expand

Commit Message

Andrew Jones Oct. 21, 2022, 10:59 a.m. UTC
Improve isa2hwcap[] by removing it from static storage, as
riscv_fill_hwcap() is only called once, and by reducing its size
from 256 bytes to 26. The latter improvement is possible because
isa2hwcap[] will never be indexed with capital letters and we can
precompute the offsets from 'a'.

No functional change intended.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
---
 arch/riscv/kernel/cpufeature.c | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

Comments

Conor Dooley Oct. 23, 2022, 7:28 p.m. UTC | #1
On Fri, Oct 21, 2022 at 12:59:03PM +0200, Andrew Jones wrote:
> Improve isa2hwcap[] by removing it from static storage, as
> riscv_fill_hwcap() is only called once, and by reducing its size
> from 256 bytes to 26. The latter improvement is possible because
> isa2hwcap[] will never be indexed with capital letters and we can
> precompute the offsets from 'a'.

Hey Drew, couple questions for you - mostly due to naivety I think..

How do we know that isa2hwcap will never interact with capital letters?
It pulls the isa string from dt and the no-capitals enforcement comes
from there since one with capitals is invalid? I didn't dig particularly
deeply into the code, but is there a risk that we regress some user that
has a dt with capitals in the isa string? Or is that a "your dt was wrong
and you're out-of-tree so that's your problem" situation?

Secondly, in the UAPI header, the COMPAT_HWCAP_ISA_FOO defines are
computed as I - A rather than i - a. Should those be changed too for the
sake of consistently using the lowercase everywhere, or do you think
that doesn't really matter?

Thanks,
Conor.

> 
> No functional change intended.
> 
> Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
> ---
>  arch/riscv/kernel/cpufeature.c | 20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
> 
> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index 694267d1fe81..4677320d7e31 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -74,15 +74,15 @@ void __init riscv_fill_hwcap(void)
>  	const char *isa;
>  	char print_str[NUM_ALPHA_EXTS + 1];
>  	int i, j, rc;
> -	static unsigned long isa2hwcap[256] = {0};
> +	unsigned long isa2hwcap[26] = {0};
>  	unsigned long hartid;
>  
> -	isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
> -	isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
> -	isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
> -	isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
> -	isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
> -	isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;
> +	isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
> +	isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
> +	isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
> +	isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
> +	isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
> +	isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
>  
>  	elf_hwcap = 0;
>  
> @@ -196,8 +196,10 @@ void __init riscv_fill_hwcap(void)
>  			if (unlikely(ext_err))
>  				continue;
>  			if (!ext_long) {
> -				this_hwcap |= isa2hwcap[(unsigned char)(*ext)];
> -				set_bit(*ext - 'a', this_isa);
> +				int nr = *ext - 'a';
> +
> +				this_hwcap |= isa2hwcap[nr];
> +				set_bit(nr, this_isa);
>  			} else {
>  				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
>  				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);
> -- 
> 2.37.3
> 
> 
> _______________________________________________
> linux-riscv mailing list
> linux-riscv@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-riscv
Andrew Jones Oct. 24, 2022, 6:48 a.m. UTC | #2
On Sun, Oct 23, 2022 at 08:28:20PM +0100, Conor Dooley wrote:
> On Fri, Oct 21, 2022 at 12:59:03PM +0200, Andrew Jones wrote:
> > Improve isa2hwcap[] by removing it from static storage, as
> > riscv_fill_hwcap() is only called once, and by reducing its size
> > from 256 bytes to 26. The latter improvement is possible because
> > isa2hwcap[] will never be indexed with capital letters and we can
> > precompute the offsets from 'a'.
> 
> Hey Drew, couple questions for you - mostly due to naivety I think..
> 
> How do we know that isa2hwcap will never interact with capital letters?
> It pulls the isa string from dt and the no-capitals enforcement comes
> from there since one with capitals is invalid? I didn't dig particularly
> deeply into the code, but is there a risk that we regress some user that
> has a dt with capitals in the isa string? Or is that a "your dt was wrong
> and you're out-of-tree so that's your problem" situation?

Our ISA string parser here in riscv_fill_hwcap() ignores non-capital
letters, see arch/riscv/kernel/cpufeature.c lines 141, 165 and 196.
(Maybe we should be noisier about that in case there are DTs out there
with capitals which aren't realizing they're being ignored.)

> 
> Secondly, in the UAPI header, the COMPAT_HWCAP_ISA_FOO defines are
> computed as I - A rather than i - a. Should those be changed too for the
> sake of consistently using the lowercase everywhere, or do you think
> that doesn't really matter?

I think I'd leave it since it doesn't change the math and I'm reluctant
to churn UAPI.

Thanks,
drew
Conor Dooley Oct. 24, 2022, 7:16 a.m. UTC | #3
On Mon, Oct 24, 2022 at 08:48:11AM +0200, Andrew Jones wrote:
> On Sun, Oct 23, 2022 at 08:28:20PM +0100, Conor Dooley wrote:
> > On Fri, Oct 21, 2022 at 12:59:03PM +0200, Andrew Jones wrote:
> > > Improve isa2hwcap[] by removing it from static storage, as
> > > riscv_fill_hwcap() is only called once, and by reducing its size
> > > from 256 bytes to 26. The latter improvement is possible because
> > > isa2hwcap[] will never be indexed with capital letters and we can
> > > precompute the offsets from 'a'.
> > 
> > Hey Drew, couple questions for you - mostly due to naivety I think..
> > 
> > How do we know that isa2hwcap will never interact with capital letters?
> > It pulls the isa string from dt and the no-capitals enforcement comes
> > from there since one with capitals is invalid? I didn't dig particularly
> > deeply into the code, but is there a risk that we regress some user that
> > has a dt with capitals in the isa string? Or is that a "your dt was wrong
> > and you're out-of-tree so that's your problem" situation?
> 
> Our ISA string parser here in riscv_fill_hwcap() ignores non-capital
> letters, see arch/riscv/kernel/cpufeature.c lines 141, 165 and 196.

Ah, I missed the one on L165, that's a bit silly... Apologies!

> (Maybe we should be noisier about that in case there are DTs out there
> with capitals which aren't realizing they're being ignored.)

Possibly, but it's not this patch that has introduced the behaviour.
Checking very briefly though, looks like it was 2a31c54be097 ("RISC-V:
Minimal parser for "riscv, isa" strings") that introduced the checks
and that was not really all that long ago..

> 
> > 
> > Secondly, in the UAPI header, the COMPAT_HWCAP_ISA_FOO defines are
> > computed as I - A rather than i - a. Should those be changed too for the
> > sake of consistently using the lowercase everywhere, or do you think
> > that doesn't really matter?
> 
> I think I'd leave it since it doesn't change the math and I'm reluctant
> to churn UAPI.

Yeah, figured you'd be of that opinion.
Anyways, only "issue" I had was my own confusion so
Reviewed-by: Conor Dooley <conor.dooley@microchip.com>

Thanks...
diff mbox series

Patch

diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
index 694267d1fe81..4677320d7e31 100644
--- a/arch/riscv/kernel/cpufeature.c
+++ b/arch/riscv/kernel/cpufeature.c
@@ -74,15 +74,15 @@  void __init riscv_fill_hwcap(void)
 	const char *isa;
 	char print_str[NUM_ALPHA_EXTS + 1];
 	int i, j, rc;
-	static unsigned long isa2hwcap[256] = {0};
+	unsigned long isa2hwcap[26] = {0};
 	unsigned long hartid;
 
-	isa2hwcap['i'] = isa2hwcap['I'] = COMPAT_HWCAP_ISA_I;
-	isa2hwcap['m'] = isa2hwcap['M'] = COMPAT_HWCAP_ISA_M;
-	isa2hwcap['a'] = isa2hwcap['A'] = COMPAT_HWCAP_ISA_A;
-	isa2hwcap['f'] = isa2hwcap['F'] = COMPAT_HWCAP_ISA_F;
-	isa2hwcap['d'] = isa2hwcap['D'] = COMPAT_HWCAP_ISA_D;
-	isa2hwcap['c'] = isa2hwcap['C'] = COMPAT_HWCAP_ISA_C;
+	isa2hwcap['i' - 'a'] = COMPAT_HWCAP_ISA_I;
+	isa2hwcap['m' - 'a'] = COMPAT_HWCAP_ISA_M;
+	isa2hwcap['a' - 'a'] = COMPAT_HWCAP_ISA_A;
+	isa2hwcap['f' - 'a'] = COMPAT_HWCAP_ISA_F;
+	isa2hwcap['d' - 'a'] = COMPAT_HWCAP_ISA_D;
+	isa2hwcap['c' - 'a'] = COMPAT_HWCAP_ISA_C;
 
 	elf_hwcap = 0;
 
@@ -196,8 +196,10 @@  void __init riscv_fill_hwcap(void)
 			if (unlikely(ext_err))
 				continue;
 			if (!ext_long) {
-				this_hwcap |= isa2hwcap[(unsigned char)(*ext)];
-				set_bit(*ext - 'a', this_isa);
+				int nr = *ext - 'a';
+
+				this_hwcap |= isa2hwcap[nr];
+				set_bit(nr, this_isa);
 			} else {
 				SET_ISA_EXT_MAP("sscofpmf", RISCV_ISA_EXT_SSCOFPMF);
 				SET_ISA_EXT_MAP("svpbmt", RISCV_ISA_EXT_SVPBMT);