From patchwork Thu Jul 27 23:26:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Brown X-Patchwork-Id: 13330974 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 560E5C00528 for ; Thu, 27 Jul 2023 23:27:34 +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:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=4zLA6bXvgoveR+Vhu9Gb/2sczxrzvgbMn7sZyXkL7lM=; b=4qBadSfZWB4HCm lX0uV2tCcWQ+KJ00xEFcQh4qeV2WaOc1m5v1f9NgiRd7v2zm6BcQw5vJrt8IuVPgvACXa4R1f4mW+ s3FsGuZPupa8YnUqoSzA0LZIwMGn++fmAtkrrF8sjogV1Vq55MkLmE4/aof6R15RDA35jJXKsUTEC wtZxz9g29c3/DT0tAiDM5fEHGUv2AveXKd1ZOShHX+TBg1DrAq23YNK52oTKu13CEcQp/Scf7MA04 OGzxL5C7Fzkv9gMTjpQYqPpjoqvmm2n47YGcsTt8oAW+e0KjLPrkLEhe358N0RXzau1BNvAlofndA DYNLMWx97DHau9plrhYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qPANy-000s4V-2c; Thu, 27 Jul 2023 23:27:10 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qPANn-000rzu-1u for linux-arm-kernel@lists.infradead.org; Thu, 27 Jul 2023 23:27:00 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DA93261F8B; Thu, 27 Jul 2023 23:26:58 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D6DD7C433CC; Thu, 27 Jul 2023 23:26:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690500418; bh=17quVgwOUPp97Iqg3owW8+YLOqublPdMuTjqZWkARPo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nkjp21yMDGAvaeHDkJINc7Yk4jYHJd6TVWwyVsWQ34LHn+p66SHnj9bdZLBqXAsbM oqCSS8+IEOyZaUy+EikWmvjemOoC3hXfqqwqhN0PM31fMD12PDPaaD0ksIbsizHF2h Sh+5pwlwqqCrYs3c8/dSnZf1JG+UKI8UETFh9mNi9qxMIysiu9RUcT0N1wghmtaJme xQ/QAKgsDFivbvnypgxgHtHlZQLf6Kvhj3u5PisjaLaC3HdIpHSUYBy/xJatZnOkS1 57ybyZnMwdF8b4UKHgSCNGiPeZ+DXXS53B0MhZelrrvrJMVXbA4KyrNmb7aYypDKBu Y9BNwnyhg4Wxg== From: Mark Brown Date: Fri, 28 Jul 2023 00:26:12 +0100 Subject: [PATCH v4 1/6] kselftest/arm64: Exit streaming mode after collecting signal context MIME-Version: 1.0 Message-Id: <20230728-arm64-signal-memcpy-fix-v4-1-0c1290db5d46@kernel.org> References: <20230728-arm64-signal-memcpy-fix-v4-0-0c1290db5d46@kernel.org> In-Reply-To: <20230728-arm64-signal-memcpy-fix-v4-0-0c1290db5d46@kernel.org> To: Catalin Marinas , Will Deacon , Shuah Khan , Arnaldo Carvalho de Melo Cc: linux-arm-kernel@lists.infradead.org, linux-kselftest@vger.kernel.org, Mark Brown X-Mailer: b4 0.13-dev-099c9 X-Developer-Signature: v=1; a=openpgp-sha256; l=3033; i=broonie@kernel.org; h=from:subject:message-id; bh=17quVgwOUPp97Iqg3owW8+YLOqublPdMuTjqZWkARPo=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBkwv05VrGqW153ETqB2RJgnxvIF7+LzYv/IlYjhZ6Z s8dnsWSJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZML9OQAKCRAk1otyXVSH0MWiB/ 0WLVRgq5i6hfMb4hl9vOiSFuzNvjGpyAaNmITZMvRqoaXQqNMCrVQXbcmW8BJhnEbdXq9f689krKZJ dWBcisXyxaUCfUh4TwQdOByvBjuIPRX88f9M3hgscq420v63Zdr3RTkmaFoXOFqRFI5dr0BDnveseH YSUtg6kEvpBEtEZR2rCbA3vqBGOtZLnQrf93RrUSrNA384N8IKOxSpnRauUiYRZekWZgUNOREZU/ej TlOl2k1QVVsXVM7+oRFrsP1rkJd1fUzqgRE2UfCGsk8KkDJbKp2VOribAg3KwJCEfkNZRdr5rYixX0 liQAB9Bj+61qzyJAfNPJlOj0hSZhXm X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230727_162659_714210_016D7D1A X-CRM114-Status: GOOD ( 21.12 ) 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 When we collect a signal context with one of the SME modes enabled we will have enabled that mode behind the compiler and libc's back so they may issue some instructions not valid in streaming mode, causing spurious failures. For the code prior to issuing the BRK to trigger signal handling we need to stay in streaming mode if we were already there since that's a part of the signal context the caller is trying to collect. Unfortunately this code includes a memset() which is likely to be heavily optimised and is likely to use FP instructions incompatible with streaming mode. We can avoid this happening by open coding the memset(), inserting a volatile assembly statement to avoid the compiler recognising what's being done and doing something in optimisation. This code is not performance critical so the inefficiency should not be an issue. After collecting the context we can simply exit streaming mode, avoiding these issues. Use a full SMSTOP for safety to prevent any issues appearing with ZA. Reported-by: Will Deacon Signed-off-by: Mark Brown --- .../selftests/arm64/signal/test_signals_utils.h | 25 +++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/arm64/signal/test_signals_utils.h b/tools/testing/selftests/arm64/signal/test_signals_utils.h index 222093f51b67..c7f5627171dd 100644 --- a/tools/testing/selftests/arm64/signal/test_signals_utils.h +++ b/tools/testing/selftests/arm64/signal/test_signals_utils.h @@ -60,13 +60,25 @@ static __always_inline bool get_current_context(struct tdescr *td, size_t dest_sz) { static volatile bool seen_already; + int i; + char *uc = (char *)dest_uc; assert(td && dest_uc); /* it's a genuine invocation..reinit */ seen_already = 0; td->live_uc_valid = 0; td->live_sz = dest_sz; - memset(dest_uc, 0x00, td->live_sz); + + /* + * This is a memset() but we don't want the compiler to + * optimise it into either instructions or a library call + * which might be incompatible with streaming mode. + */ + for (i = 0; i < td->live_sz; i++) { + uc[i] = 0; + __asm__ ("" : "=r" (uc[i]) : "0" (uc[i])); + } + td->live_uc = dest_uc; /* * Grab ucontext_t triggering a SIGTRAP. @@ -103,6 +115,17 @@ static __always_inline bool get_current_context(struct tdescr *td, : : "memory"); + /* + * If we were grabbing a streaming mode context then we may + * have entered streaming mode behind the system's back and + * libc or compiler generated code might decide to do + * something invalid in streaming mode, or potentially even + * the state of ZA. Issue a SMSTOP to exit both now we have + * grabbed the state. + */ + if (td->feats_supported & FEAT_SME) + asm volatile("msr S0_3_C4_C6_3, xzr"); + /* * If we get here with seen_already==1 it implies the td->live_uc * context has been used to get back here....this probably means