From patchwork Fri Nov 24 10:18:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13467568 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 91BC6C61D97 for ; Fri, 24 Nov 2023 11:31:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:From:Subject:Message-ID: References:Mime-Version:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=Zbirus4uWFMFyTzxL6oZTZELWjzRpJSP4/yKBWC6gFk=; b=Bn93FxOeHa/Xj3CfakhHYy74jy VuHDoSdacak8Ybf5HjRw4EDmC64pdrkDi0P0c18RYki6CAC42C+qBIEbutYMO7STZlQp/DgyuQ2NG NbknX0a++qrDsXEst/uLdH0Czf64+ffCzklTJWxvIJWX+f1lulD3+kDYNR6IDU4jeGwmwEzXFPizH EcpctE4NrjU6SR0+3lJDLDudzBIxCVu0Yhk2tyQtpX5F2yC5QewQCm73jnPp+/SapXXxS1lQv95Lf cLNjGT/5sNccCiiG1pm77K05ggGLTU+aedLdIgouCcRjGRGjnaIeDDK87sfW2LYKLXif97swO+haU XBrMOWAA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1r6UPX-006yUm-1S; Fri, 24 Nov 2023 11:31:51 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1r6TIx-006omk-0Q for linux-arm-kernel@lists.infradead.org; Fri, 24 Nov 2023 10:21:03 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5ccf5d22873so26882247b3.1 for ; Fri, 24 Nov 2023 02:20:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700821255; x=1701426055; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=l5ZZZnEoSFivql5UHpCJhbC6GFrfdxCztP0ALxEGa0c=; b=LiJgNhAtYMFTwngPKPza4kqe69ZIILPGfZ7p6DmboOJ1dVK3N5vLOo7eJvtU4Q6pCX e3pSSROJV7Hvho+ijSLYAt9/9zSJYsfKx1VwTcfz0dhQpLX4O6PqETfJUa358FAA1EtY xiJW14d8DQrsxZ/+h6eX4Mh/00BGjcf8n7mxWBhfOQEfEzOMyfpy8BchfVp6vs3wHT71 bfitQwqDDgpAWNQPtoMasUinqstquPeJyV/PYvf5Rg7cXVWkFBZ/s9QiMFlbYdQvUGNR C/pbyE4QxA2mbaahxzCSw2Ubo6H+R5QvM6grGRCBMa6P+DGFQOSKi+7SsP/HAm8YiiYC c1Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700821255; x=1701426055; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=l5ZZZnEoSFivql5UHpCJhbC6GFrfdxCztP0ALxEGa0c=; b=ElEMAER57sM2b9xi7x5RLqV/xvKtEYB+ohCgqXhcK4hFxHRXLDGennC+fNhn5tmmrG VfbJL/6rd++v5mzJaWZ3R1CFJpi+uNkJr66Bz/xZNLKRomf+GCnyhwmzWpyl6HqAW45S 8PwD2F/VSNKOyhyfY+2oR2HEB78D2jHFNC5ujIB/MWohGblSvvUvHygrRyUJap6zGYBa 6VQpWUjFQA/W8zKNLOTbHXoQ3eii2I6xSRTOuvmwVifWsaP/GcMfyhC4s6MocXSvKbZW HHqCSqTspUk3lkXxYhgipfaeXZjM8F74uH5vfuBVhLGK1y/nmPjAPP5T021JjrDOg88T b45w== X-Gm-Message-State: AOJu0YyWV/2bivnCagsEA/qvyzY4+uPk9mWXEzR2CFqI47c+SuAUDfIc akQYBhEAZALd75ih/S+A0SUDkErmw3OvB0CSRqXIK+2qc8ylnEJ4LnoV2kZjqX8y+xofMlNFNFB fIWzdt/7rvG/bS6ZJ41H+U5Q4yUCbyR9DBRc6ZO+p8tVf/BQG41gnJxaOEQHSeJlTwnE2nvH4OK s= X-Google-Smtp-Source: AGHT+IHc3GegYcnvfs6Zy5Nk95WyX9va0N2plqPGDWk8KBWsNceQoq83WqlwSdY8FLpEBoYWxIBHI08N X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a05:690c:845:b0:5a8:205e:1f27 with SMTP id bz5-20020a05690c084500b005a8205e1f27mr69580ywb.6.1700821254756; Fri, 24 Nov 2023 02:20:54 -0800 (PST) Date: Fri, 24 Nov 2023 11:18:58 +0100 In-Reply-To: <20231124101840.944737-41-ardb@google.com> Mime-Version: 1.0 References: <20231124101840.944737-41-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2434; i=ardb@kernel.org; h=from:subject; bh=lC4DkQ4BnH8UW4k/1dPlaimYll+z9uySAutwLfWCeZg=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JITWhYtqJ6/PNZn6a9/DTL6Hrx5rd192oPhZspLxZOGl9Z tFdP83fHaUsDGIcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAiC2cwMpzabm4ef3iSgH3f 10W68YHviguu3ujStfTbctLjEcPXxnCGv1L7VL4aRyW4GUR4TBI0eazAsKrpjsLu0tkWqZvYQyc oMAAA X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231124101840.944737-59-ardb@google.com> Subject: [PATCH v5 18/39] arm64: idreg-override: Avoid sprintf() for simple string concatenation From: Ard Biesheuvel To: linux-arm-kernel@lists.infradead.org Cc: Ard Biesheuvel , Catalin Marinas , Will Deacon , Marc Zyngier , Mark Rutland , Ryan Roberts , Anshuman Khandual , Kees Cook X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20231124_022059_198712_315688FC X-CRM114-Status: GOOD ( 16.24 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Ard Biesheuvel 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 --- arch/arm64/kernel/idreg-override.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kernel/idreg-override.c b/arch/arm64/kernel/idreg-override.c index 96fec5b7a65e..41eeca857b32 100644 --- a/arch/arm64/kernel/idreg-override.c +++ b/arch/arm64/kernel/idreg-override.c @@ -215,14 +215,15 @@ static int __init parse_nokaslr(char *unused) } early_param("nokaslr", parse_nokaslr); -static int __init find_field(const char *cmdline, +static int __init find_field(const char *cmdline, char *opt, int len, const struct ftr_set_desc *reg, int f, u64 *v) { - char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2]; - int len; + int flen = strlen(reg->fields[f].name); - len = snprintf(opt, ARRAY_SIZE(opt), "%s.%s=", - reg->name, reg->fields[f].name); + // append '=' to obtain '.=' + memcpy(opt + len, reg->fields[f].name, flen); + len += flen; + opt[len++] = '='; if (memcmp(cmdline, opt, len)) return -1; @@ -232,15 +233,21 @@ static int __init find_field(const char *cmdline, static void __init match_options(const char *cmdline) { + char opt[FTR_DESC_NAME_LEN + FTR_DESC_FIELD_LEN + 2]; int i; for (i = 0; i < ARRAY_SIZE(regs); i++) { const struct ftr_set_desc *reg = prel64_to_pointer(®s[i].reg_prel); struct arm64_ftr_override *override; + int len = strlen(reg->name); int f; override = prel64_to_pointer(®->override_prel); + // set opt[] to '.' + memcpy(opt, reg->name, len); + opt[len++] = '.'; + for (f = 0; reg->fields[f].name[0] != '\0'; f++) { u64 shift = reg->fields[f].shift; u64 width = reg->fields[f].width ?: 4; @@ -248,7 +255,7 @@ static void __init match_options(const char *cmdline) bool (*filter)(u64 val); u64 v; - if (find_field(cmdline, reg, f, &v)) + if (find_field(cmdline, opt, len, reg, f, &v)) continue; /*