Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3179958pxb; Mon, 18 Apr 2022 18:15:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyXN10CEsrjQPJpdm1Lqoz8GdUPjVqaeQZzg1QFOumNI/LsYaNsRYPabfhv7iV8/5vg5gpO X-Received: by 2002:a17:90a:5298:b0:1ca:7fb3:145 with SMTP id w24-20020a17090a529800b001ca7fb30145mr15736981pjh.200.1650330946752; Mon, 18 Apr 2022 18:15:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650330946; cv=none; d=google.com; s=arc-20160816; b=fQX4iUXRE3exUBAj29lonOk1gtccKz5wxhpDhhw/mRDXi+10KCRKKJ8eLA894YX/ci XhRfpzk5gmCpl1YToz7QXjy4ABRThPPoKfoGLm2WQn84hMEhqezckbc0ePC/L5qjz0NO jMp11MwviRaprHjejrvCqw6nRKFmBc+WMUdaP0Q/2gKXTBBWHLjlW+uSGnYErI72HIDR sbh6oaKvrlXsX4Gm1oc6BUsiWS6D32GcExOpLacHYWADldAZ9X5MTQJOTIRf6dyi4E6E KfBnJGkCJYrpQhJ+vSn6s6ejdY26fuJO1g4oH+VSi5NwE0w/FQieEH8B/D0yjcMiiJQD dKNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5YqO2xIE9oB1WKirzVrbKf5bjxbxbbo18iYgGO646Cg=; b=wi5PYdOUiZAX7Bd2VrhvZTdW/Eei8msrMcY0OA3U3iwFdq7QI7IzH76cwj5AYtEsWj ZbMlMPp44NxVuge7uLQnzAkq0Hek+DFD5cqV5lcZ/mIyfOilPBBWB3osKqb4zFRJO8S9 Ryf0j/0i4SKHMX53nPwLpOSspxT2Hz5xEUFl391UwgNVvd5MLl763qWYOp0aW57Wi5zV D/WXnDGixOXHPQe2qIdQ2pHcNT5GYIlgwsDPD5mTrUubbd9ZY5N/eCXaMd9rJVF4VHZm FqViRGmjLhe4tHERstdVkWUVEw81UdPVRXQQpjwzD7lqPyLWlJZRWEKvbgiqI/dDxj5O 4NQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KjU+LTz4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 72-20020a63014b000000b003995e6c5133si10273443pgb.442.2022.04.18.18.15.30; Mon, 18 Apr 2022 18:15:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=KjU+LTz4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243801AbiDRN3H (ORCPT + 99 others); Mon, 18 Apr 2022 09:29:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41176 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241051AbiDRNF2 (ORCPT ); Mon, 18 Apr 2022 09:05:28 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 053F429828; Mon, 18 Apr 2022 05:46:06 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B5A80B80E4B; Mon, 18 Apr 2022 12:46:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2CF8BC385A7; Mon, 18 Apr 2022 12:46:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650285963; bh=h9/5KSj6OJA2aQ/9/6+/e4MfRq0HbHkAqRzBeWOfW0Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KjU+LTz4eLWgfy59wHFLaBhJHYK0R8RR0RQ7lrgu3SB6TG/31+qk3hXoX0avZM+6z L2FvokOe4OI7lz8v83N1NMMBx2fsbj07CPC5pU21p7S1W8t9mDrtSi22sdKSetVebB 4pu6c9sIj8o5i/vcoONrc8yGx59QaNWLB3kq0SXY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Joey Gouly , Mark Rutland , Catalin Marinas , Will Deacon , Sasha Levin Subject: [PATCH 4.19 19/32] arm64: alternatives: mark patch_alternative() as `noinstr` Date: Mon, 18 Apr 2022 14:13:59 +0200 Message-Id: <20220418121127.688055795@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121127.127656835@linuxfoundation.org> References: <20220418121127.127656835@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Joey Gouly [ Upstream commit a2c0b0fbe01419f8f5d1c0b9c581631f34ffce8b ] The alternatives code must be `noinstr` such that it does not patch itself, as the cache invalidation is only performed after all the alternatives have been applied. Mark patch_alternative() as `noinstr`. Mark branch_insn_requires_update() and get_alt_insn() with `__always_inline` since they are both only called through patch_alternative(). Booting a kernel in QEMU TCG with KCSAN=y and ARM64_USE_LSE_ATOMICS=y caused a boot hang: [ 0.241121] CPU: All CPU(s) started at EL2 The alternatives code was patching the atomics in __tsan_read4() from LL/SC atomics to LSE atomics. The following fragment is using LL/SC atomics in the .text section: | <__tsan_unaligned_read4+304>: ldxr x6, [x2] | <__tsan_unaligned_read4+308>: add x6, x6, x5 | <__tsan_unaligned_read4+312>: stxr w7, x6, [x2] | <__tsan_unaligned_read4+316>: cbnz w7, <__tsan_unaligned_read4+304> This LL/SC atomic sequence was to be replaced with LSE atomics. However since the alternatives code was instrumentable, __tsan_read4() was being called after only the first instruction was replaced, which led to the following code in memory: | <__tsan_unaligned_read4+304>: ldadd x5, x6, [x2] | <__tsan_unaligned_read4+308>: add x6, x6, x5 | <__tsan_unaligned_read4+312>: stxr w7, x6, [x2] | <__tsan_unaligned_read4+316>: cbnz w7, <__tsan_unaligned_read4+304> This caused an infinite loop as the `stxr` instruction never completed successfully, so `w7` was always 0. Signed-off-by: Joey Gouly Cc: Mark Rutland Cc: Catalin Marinas Cc: Will Deacon Link: https://lore.kernel.org/r/20220405104733.11476-1-joey.gouly@arm.com Signed-off-by: Will Deacon Signed-off-by: Sasha Levin --- arch/arm64/kernel/alternative.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kernel/alternative.c b/arch/arm64/kernel/alternative.c index 0d345622bbba..3747c8d87bdb 100644 --- a/arch/arm64/kernel/alternative.c +++ b/arch/arm64/kernel/alternative.c @@ -42,7 +42,7 @@ struct alt_region { /* * Check if the target PC is within an alternative block. */ -static bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc) +static __always_inline bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc) { unsigned long replptr = (unsigned long)ALT_REPL_PTR(alt); return !(pc >= replptr && pc <= (replptr + alt->alt_len)); @@ -50,7 +50,7 @@ static bool branch_insn_requires_update(struct alt_instr *alt, unsigned long pc) #define align_down(x, a) ((unsigned long)(x) & ~(((unsigned long)(a)) - 1)) -static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr) +static __always_inline u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnptr) { u32 insn; @@ -95,7 +95,7 @@ static u32 get_alt_insn(struct alt_instr *alt, __le32 *insnptr, __le32 *altinsnp return insn; } -static void patch_alternative(struct alt_instr *alt, +static noinstr void patch_alternative(struct alt_instr *alt, __le32 *origptr, __le32 *updptr, int nr_inst) { __le32 *replptr; -- 2.35.1