Skip to content

Commit 060260a

Browse files
ardbiesheuvelwilldeacon
authored andcommitted
arm64: idreg-override: Avoid sprintf() for simple string concatenation
Instead of using sprintf() with the "%s.%s=" format, where the first string argument is always the same in the inner loop of match_options(), use simple memcpy() for string concatenation, and move the first copy to the outer loop. This removes the dependency on sprintf(), which will be difficult to fulfil when 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-61-ardb@google.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent bcf1eed commit 060260a

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

arch/arm64/kernel/idreg-override.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,15 @@ static int __init parse_nokaslr(char *unused)
206206
}
207207
early_param("nokaslr", parse_nokaslr);
208208

209-
static int __init find_field(const char *cmdline,
209+
static int __init find_field(const char *cmdline, char *opt, int len,
210210
const struct ftr_set_desc *reg, int f, u64 *v)
211211
{
212-
char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
213-
int len;
212+
int flen = strlen(reg->fields[f].name);
214213

215-
len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=",
216-
reg->name, reg->fields[f].name);
214+
// append '<fieldname>=' to obtain '<name>.<fieldname>='
215+
memcpy(opt + len, reg->fields[f].name, flen);
216+
len += flen;
217+
opt[len++] = '=';
217218

218219
if (memcmp(cmdline, opt, len))
219220
return -1;
@@ -223,23 +224,29 @@ static int __init find_field(const char *cmdline,
223224

224225
static void __init match_options(const char *cmdline)
225226
{
227+
char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2];
226228
int i;
227229

228230
for (i = 0; i < ARRAY_SIZE(regs); i++) {
229231
const struct ftr_set_desc *reg = prel64_pointer(regs[i].reg);
230232
struct arm64_ftr_override *override;
233+
int len = strlen(reg->name);
231234
int f;
232235

233236
override = prel64_pointer(reg->override);
234237

238+
// set opt[] to '<name>.'
239+
memcpy(opt, reg->name, len);
240+
opt[len++] = '.';
241+
235242
for (f = 0; reg->fields[f].name[0] != '\0'; f++) {
236243
u64 shift = reg->fields[f].shift;
237244
u64 width = reg->fields[f].width ?: 4;
238245
u64 mask = GENMASK_ULL(shift + width - 1, shift);
239246
bool (*filter)(u64 val);
240247
u64 v;
241248

242-
if (find_field(cmdline, reg, f, &v))
249+
if (find_field(cmdline, opt, len, reg, f, &v))
243250
continue;
244251

245252
/*

0 commit comments

Comments
 (0)