From patchwork Tue Sep 12 14:16:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 13381863 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 18633CA0EF2 for ; Tue, 12 Sep 2023 15:30:29 +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=xs6o+T3XhtODyqLxVZXVq8T/BFe0/95q061oYDWVna0=; b=jcxMmaXpbV4QrbS2wpH6md3afX 5d7v3YAqFWLZvHSi6mJJprht/GNA0ewdzvMC7Zn+oiNhBvpzjRdym+hZbDHtBdPXG8VbjCQHUcTdG 3G/RkFTwxuA3RvCg+FpfuwvDdC8uNWsfp25Htk+nX5HAwuXYfU28ObSR1prlCaFSBUTcYGAch6otC RDyZ0kFZqsl0IpBaiYlI1oE6Y+jBtKSGSR9YKtLiV5B24bZGa9D5LXN37K6d2KfcQ5w/11RHFmBbK n03IFkLiFaHgy/gJ6/1DWSKGpkZVg9QaeCnVj/EYKJkEfbyqrENCQVzHqQJOiMtlbwP90ODkXuOse iYh6m0PA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qg5LP-003igF-0r; Tue, 12 Sep 2023 15:30:27 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qg4DH-003WFS-33 for linux-arm-kernel@lists.infradead.org; Tue, 12 Sep 2023 14:18:07 +0000 Received: by mail-wm1-x349.google.com with SMTP id 5b1f17b1804b1-4011fa32e99so43408345e9.0 for ; Tue, 12 Sep 2023 07:17:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694528277; x=1695133077; 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=Pvdt5Oe1T19y8sApBOZg/loa9q7iUxAgj+0AqF1CeGg=; b=b8nJvdgUNR+y6dljIaO7xAC2HgdnmVjgi2LfMcbAxxD1be5Q2TkHoyaR/ZodZKq6SE bbEpnAUKe/NE1mElMV4y8lQnpKfljyNFG1TqVZ6+xo3mLl6aadxHZLvpQHsACiAfFk4a UeyrCAl1Vlg6KybUwB7hReyyeztZhfDpJqJD8IBz6o9KScQ6RHO6PSfr3J38fDlghSFB 8KloqKWLKzrVetJsu+3LsTBUumZ7oHJqOSNv3Ji04AOD2xx5g3f/lT4xnLQ6BL3MSiLL 19H5DrLMM2CQMl2WouChmD08W7RRijwRvq7cGI2mp8nty6QNV/f15KgP4JffuK85/ndG 74tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694528277; x=1695133077; 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=Pvdt5Oe1T19y8sApBOZg/loa9q7iUxAgj+0AqF1CeGg=; b=v4c7CN3nEXDXfUcuoei/VsJiHJSud26lY2yBA5F+zTh3YZ6rWqDpBKAGopX3cZqkM+ UapKMf6kih47g5ojBUdSAOcs0QwWAtPIdB7uryBPifTffaSsjL1zl9ceP1J8bwP01Xgn EqtMmYOk+WjRh8sZEh2owp2cONroCp7r+BbcgI8lYbjit3ZcMFKwEKjQnotZOJxIzKCS BkWfFkHcy95qrcT3eMf+TbzJBuNOH7OlMIrNsflcnSS4bv6owqJ225TO8KjEUEfxHLqs Bd2xKE96bCiKigA50PQrhYNB2esjLYJsGNCd4r/z4HQLnNxnQCM8Y3szFt89kgYFgPSV By+w== X-Gm-Message-State: AOJu0Yymxj+nhdRhQDy5ONxOxIguxmy52GXDG57LmjbFcABgKmU2Y8+V /3Gy9uXdqC+pKMpbEwq6L5jsvNgkJXZIPFZ6fz9NeI+/8Dh5XuwttQAvo+Fot8lulIfeUVwIp8M llsSlAiosSUuB7ugLF/KxJWjJ7Il/mdBMvU8gUyPXEw5RjOGny62tsm2sYs0AmbrmglHKyKAriw 8= X-Google-Smtp-Source: AGHT+IEDnRLHFP0vznFo5jDa0GsBziy1byoBu7OS7lDJJzGkX6MAuqjU7f2zKsSIysFC/7L7Pqhx+LnH X-Received: from palermo.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:118a]) (user=ardb job=sendgmr) by 2002:a5d:4388:0:b0:317:6b94:b700 with SMTP id i8-20020a5d4388000000b003176b94b700mr147658wrq.9.1694528277588; Tue, 12 Sep 2023 07:17:57 -0700 (PDT) Date: Tue, 12 Sep 2023 14:16:08 +0000 In-Reply-To: <20230912141549.278777-63-ardb@google.com> Mime-Version: 1.0 References: <20230912141549.278777-63-ardb@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=2430; i=ardb@kernel.org; h=from:subject; bh=t58xkJ8N9gZCd7n1STbdJu5HNYTUXfHvSs7cQZ8K738=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIZWhaIVNRdR8z2kVf+SPCUY7Tc3c9cn+udFeLa0bLbcth Q6selbaUcrCIMbBICumyCIw+++7nacnStU6z5KFmcPKBDKEgYtTACayVpaRoUsplc3kIMsLtc+M Zi4mRrsfn/zh8IXJ+k6Cp0Tho+q3JowMO758XP1/ckmM4RqLT4s+vu3y2xyXUqfd3LaKozPoDq8 +IwA= X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230912141549.278777-81-ardb@google.com> Subject: [PATCH v4 18/61] 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 , Joey Gouly X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230912_071800_014531_723CBAAB X-CRM114-Status: GOOD ( 17.65 ) 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; /*