From: Conor Dooley <conor@kernel.org>
To: Yangyu Chen <cyy@cyyself.name>
Cc: Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
Wende Tan <twd2.me@gmail.com>, Soha Jin <soha@lohu.info>,
Hongren Zheng <i@zenithal.me>,
Andrew Jones <ajones@ventanamicro.com>
Subject: Re: [PATCH 1/2] riscv: allow case-insensitive ISA string parsing
Date: Wed, 26 Apr 2023 19:54:39 +0100 [thread overview]
Message-ID: <20230426-porthole-wronged-d5a6a3b89596@spud> (raw)
In-Reply-To: <tencent_63090269FF399AE30AC774848C344EF2F10A@qq.com>
[-- Attachment #1.1: Type: text/plain, Size: 6735 bytes --]
(+CC Drew)
Hey Yangyu,
One meta-level comment - can you submit this patch + my dt-bindings
patch as a v2?
Some comments below.
On Tue, Apr 25, 2023 at 08:00:15PM +0800, Yangyu Chen wrote:
> According to RISC-V ISA specification, the ISA naming strings are case
> insensitive. The kernel docs require the riscv,isa string must be all
> lowercase to simplify parsing currently. However, this limitation is not
> consistent with RISC-V ISA Spec.
Please remove the above and cite ACPI's case-insensitivity as the
rationale for this change.
> This patch modifies the ISA string parser in the kernel to support
> case-insensitive ISA string parsing. It replaces `strncmp` with
> `strncasecmp`, replaces `islower` with `isalpha`, and wraps the
> dereferenced char in the parser with `tolower`.
>
> Signed-off-by: Yangyu Chen <cyy@cyyself.name>
> ---
> arch/riscv/kernel/cpu.c | 6 ++++--
> arch/riscv/kernel/cpufeature.c | 20 ++++++++++----------
> 2 files changed, 14 insertions(+), 12 deletions(-)
>
> diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c
> index 8400f0cc9704..531c76079b73 100644
> --- a/arch/riscv/kernel/cpu.c
> +++ b/arch/riscv/kernel/cpu.c
> @@ -4,6 +4,7 @@
> */
>
> #include <linux/cpu.h>
> +#include <linux/ctype.h>
> #include <linux/init.h>
> #include <linux/seq_file.h>
> #include <linux/of.h>
> @@ -41,7 +42,7 @@ int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
> pr_warn("CPU with hartid=%lu has no \"riscv,isa\" property\n", *hart);
> return -ENODEV;
> }
> - if (isa[0] != 'r' || isa[1] != 'v') {
> + if (tolower(isa[0]) != 'r' || tolower(isa[1]) != 'v') {
> pr_warn("CPU with hartid=%lu has an invalid ISA of \"%s\"\n", *hart, isa);
> return -ENODEV;
I don't understand why this is even here in the first place. I'd be
inclined to advocate for it's entire removal. Checking *only* that there
is an "rv" in that string seems pointless to me. If you're on a 64-bit
kernel and the node has riscv,isa = "rv32ima" it's gonna say it is okay?
Drew what do you think?
> }
> @@ -228,7 +229,8 @@ static void print_isa(struct seq_file *f, const char *isa)
>
> seq_puts(f, "isa\t\t: ");
> /* Print the rv[64/32] part */
> - seq_write(f, isa, 4);
> + for (i = 0; i < 4; i++)
> + seq_putc(f, tolower(isa[i]));
As was pointed out elsewhere, we shouldn't parse the dt to construct
this in the first place. We know what kernel we are running on, so we
should instead do write "rv" into the string & do:
string = "rv"
if IS_ENABLED(32-bit):
isa.append("32")
else
isa.append("64")
See the link below, and Drew's comment on that:
https://lore.kernel.org/all/20230424194911.264850-3-heiko.stuebner@vrull.eu/
I'm fine with this change for now in isolation, but it ideally will be
replaced with something that doesn't touch the DT/ACPI for this
information.
> for (i = 0; i < sizeof(base_riscv_exts); i++) {
> if (__riscv_isa_extension_available(NULL, base_riscv_exts[i] - 'a'))
> /* Print only enabled the base ISA extensions */
> diff --git a/arch/riscv/kernel/cpufeature.c b/arch/riscv/kernel/cpufeature.c
> index 59d58ee0f68d..c01dd144addc 100644
> --- a/arch/riscv/kernel/cpufeature.c
> +++ b/arch/riscv/kernel/cpufeature.c
> @@ -120,10 +120,10 @@ void __init riscv_fill_hwcap(void)
>
> temp = isa;
> #if IS_ENABLED(CONFIG_32BIT)
> - if (!strncmp(isa, "rv32", 4))
> + if (!strncasecmp(isa, "rv32", 4))
> isa += 4;
> #elif IS_ENABLED(CONFIG_64BIT)
> - if (!strncmp(isa, "rv64", 4))
> + if (!strncasecmp(isa, "rv64", 4))
> isa += 4;
> #endif
If you're already modifying these lines, why not convert the ifdeffery
into something like
if (IS_ENABLED(CONFIG_32BIT) && !strncasecmp(isa, "rv32", 4))
isa += 4;
else if (!strncasecmp(isa, "rv64", 4))
isa += 4;
> /* The riscv,isa DT property must start with rv64 or rv32 */
> @@ -135,7 +135,7 @@ void __init riscv_fill_hwcap(void)
> const char *ext_end = isa;
> bool ext_long = false, ext_err = false;
>
> - switch (*ext) {
> + switch (tolower(*ext)) {
Is there merit in just converting the entire string to lower-case in the
first place rather than having to fiddle with every single time we want
to do a comparison here?
> case 's':
> /**
> * Workaround for invalid single-letter 's' & 'u'(QEMU).
> @@ -143,7 +143,7 @@ void __init riscv_fill_hwcap(void)
> * not valid ISA extensions. It works until multi-letter
> * extension starting with "Su" appears.
> */
> - if (ext[-1] != '_' && ext[1] == 'u') {
> + if (ext[-1] != '_' && tolower(ext[1]) == 'u') {
> ++isa;
> ext_err = true;
> break;
> @@ -154,7 +154,7 @@ void __init riscv_fill_hwcap(void)
> ext_long = true;
> /* Multi-letter extension must be delimited */
> for (; *isa && *isa != '_'; ++isa)
> - if (unlikely(!islower(*isa)
> + if (unlikely(!isalpha(*isa)
> && !isdigit(*isa)))
This collapses to isalnum() I think, no?
Cheers,
Conor.
> ext_err = true;
> /* Parse backwards */
> @@ -166,7 +166,7 @@ void __init riscv_fill_hwcap(void)
> /* Skip the minor version */
> while (isdigit(*--ext_end))
> ;
> - if (ext_end[0] != 'p'
> + if (tolower(ext_end[0]) != 'p'
> || !isdigit(ext_end[-1])) {
> /* Advance it to offset the pre-decrement */
> ++ext_end;
> @@ -178,7 +178,7 @@ void __init riscv_fill_hwcap(void)
> ++ext_end;
> break;
> default:
> - if (unlikely(!islower(*ext))) {
> + if (unlikely(!isalpha(*ext))) {
> ext_err = true;
> break;
> }
> @@ -188,7 +188,7 @@ void __init riscv_fill_hwcap(void)
> /* Skip the minor version */
> while (isdigit(*++isa))
> ;
> - if (*isa != 'p')
> + if (tolower(*isa) != 'p')
> break;
> if (!isdigit(*++isa)) {
> --isa;
> @@ -205,7 +205,7 @@ void __init riscv_fill_hwcap(void)
> #define SET_ISA_EXT_MAP(name, bit) \
> do { \
> if ((ext_end - ext == sizeof(name) - 1) && \
> - !memcmp(ext, name, sizeof(name) - 1) && \
> + !strncasecmp(ext, name, sizeof(name) - 1) && \
> riscv_isa_extension_check(bit)) \
> set_bit(bit, this_isa); \
> } while (false) \
> @@ -213,7 +213,7 @@ void __init riscv_fill_hwcap(void)
> if (unlikely(ext_err))
> continue;
> if (!ext_long) {
> - int nr = *ext - 'a';
> + int nr = tolower(*ext) - 'a';
>
> if (riscv_isa_extension_check(nr)) {
> this_hwcap |= isa2hwcap[nr];
> --
> 2.40.0
>
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 228 bytes --]
[-- Attachment #2: Type: text/plain, Size: 161 bytes --]
_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv
next prev parent reply other threads:[~2023-04-26 18:54 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20230425120016.187010-1-cyy@cyyself.name>
2023-04-25 12:00 ` [PATCH 1/2] riscv: allow case-insensitive ISA string parsing Yangyu Chen
2023-04-25 12:45 ` Yangyu Chen
2023-04-26 18:54 ` Conor Dooley [this message]
2023-04-27 7:53 ` Andrew Jones
2023-04-27 9:04 ` Conor Dooley
2023-04-27 9:25 ` Andrew Jones
2023-04-27 9:36 ` Yangyu Chen
2023-04-27 10:00 ` Conor Dooley
2023-04-27 12:47 ` Yangyu Chen
2023-04-27 17:28 ` Conor Dooley
2023-04-25 12:00 ` [PATCH 2/2] docs: dt: allow case-insensitive RISC-V ISA string Yangyu Chen
2023-04-25 13:32 ` Conor Dooley
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230426-porthole-wronged-d5a6a3b89596@spud \
--to=conor@kernel.org \
--cc=ajones@ventanamicro.com \
--cc=aou@eecs.berkeley.edu \
--cc=cyy@cyyself.name \
--cc=i@zenithal.me \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=paul.walmsley@sifive.com \
--cc=soha@lohu.info \
--cc=twd2.me@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox