arm64: idreg-override: Avoid parameq() and parameqn()
authorArd Biesheuvel <ardb@kernel.org>
Wed, 29 Nov 2023 11:16:12 +0000 (12:16 +0100)
committerWill Deacon <will@kernel.org>
Tue, 12 Dec 2023 11:13:52 +0000 (11:13 +0000)
The only way parameq() and parameqn() deviate from the ordinary string
and memory routines is that they ignore the difference between dashes
and underscores.

Since we copy each command line argument into a buffer before passing it
to parameq() and parameqn() numerous times, let's just convert all
dashes to underscores just once, and update the alias array accordingly.

This also helps reduce the dependency on kernel APIs that are no longer
available once we move this code into the early mini C runtime.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20231129111555.3594833-59-ardb@google.com
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/kernel/idreg-override.c

index ca1b8d2dbe9919ecd2ad8cf938debe6701d1a632..1eca93446345e869a33885486daf32624bae91cf 100644 (file)
@@ -185,8 +185,8 @@ static const struct {
        char    alias[FTR_ALIAS_NAME_LEN];
        char    feature[FTR_ALIAS_OPTION_LEN];
 } aliases[] __initconst = {
-       { "kvm-arm.mode=nvhe",          "id_aa64mmfr1.vh=0" },
-       { "kvm-arm.mode=protected",     "id_aa64mmfr1.vh=0" },
+       { "kvm_arm.mode=nvhe",          "id_aa64mmfr1.vh=0" },
+       { "kvm_arm.mode=protected",     "id_aa64mmfr1.vh=0" },
        { "arm64.nosve",                "id_aa64pfr0.sve=0" },
        { "arm64.nosme",                "id_aa64pfr1.sme=0" },
        { "arm64.nobti",                "id_aa64pfr1.bt=0" },
@@ -215,7 +215,7 @@ static int __init find_field(const char *cmdline,
        len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
                       reg->name, reg->fields[f].name);
 
-       if (!parameqn(cmdline, opt, len))
+       if (memcmp(cmdline, opt, len))
                return -1;
 
        return kstrtou64(cmdline + len, 0, v);
@@ -272,23 +272,29 @@ static __init void __parse_cmdline(const char *cmdline, bool parse_aliases)
 
                cmdline = skip_spaces(cmdline);
 
-               for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++);
-               if (!len)
+               /* terminate on "--" appearing on the command line by itself */
+               if (cmdline[0] == '-' && cmdline[1] == '-' && isspace(cmdline[2]))
                        return;
 
-               len = min(len, ARRAY_SIZE(buf) - 1);
-               memcpy(buf, cmdline, len);
-               buf[len] = '\0';
-
-               if (strcmp(buf, "--") == 0)
+               for (len = 0; cmdline[len] && !isspace(cmdline[len]); len++) {
+                       if (len >= sizeof(buf) - 1)
+                               break;
+                       if (cmdline[len] == '-')
+                               buf[len] = '_';
+                       else
+                               buf[len] = cmdline[len];
+               }
+               if (!len)
                        return;
 
+               buf[len] = 0;
+
                cmdline += len;
 
                match_options(buf);
 
                for (i = 0; parse_aliases && i < ARRAY_SIZE(aliases); i++)
-                       if (parameq(buf, aliases[i].alias))
+                       if (!memcmp(buf, aliases[i].alias, len + 1))
                                __parse_cmdline(aliases[i].feature, false);
        } while (1);
 }