Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp3322455pxb; Mon, 18 Apr 2022 23:03:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyowArFSuNWEZiwbogelB9TC85A7GW40xxPCmkMk4JFFXyU1odMg8u9u5cpm8UlF2xAoyvd X-Received: by 2002:a17:902:cf0d:b0:156:1cc:f08f with SMTP id i13-20020a170902cf0d00b0015601ccf08fmr14026287plg.42.1650348231138; Mon, 18 Apr 2022 23:03:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650348231; cv=none; d=google.com; s=arc-20160816; b=fCIjuLgpaMO+0dusm/hSlr+TvqVt/0SxfQkUYAW3dFvgr1kc/OsmB4QjVZmCbMsPnG XLXINEYApGVKBjxq8lPOxjIusYxB1g3JAWh9Ere1HnVqwLvNRQUeabV/nMN2gDIOftwA hQ8ZBNW1BFkGhdn/p+eA/Q5pgjhWzUBl/2Ud/6+YR8FHNdBwn0/D+DN9Lz15WjrQNvZJ mH99qyoO7U7YFIqywvq9BWi7mlELkGYDkgDLJgQ7ehVTnYwNZCnp4+3REG9Jfcf/3ycN kCd+7EgcyGrlWd7fUeywMwlRlA37PKaBhk2F5WaxlVQljmdBohCzm1oectRmHUJewCBt NrIg== 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=N1cDszV/jmL0yq6LdXStbTF8jRn3DGuY2MKl5jNIL+k=; b=OA3d7PQqFEyTqAoncVsA+FaklPtor+NS4AJuGEY/+RYT1LR+cV2AvKhBSUuTivAeVG aVwR1zDfAuFVRZgpWpWZTdLX03KICMYhPp1EUNDVab0KAUqcwMlWtdM9/Jpe1QxXFT2C hIJlE/jXFdtx09fWpWz/g/fVZR9fVUrNAxNhijpVdm8Ga7O7wNw5sYGmq95uvzjaNHus XP4rTtEYehtQe8wULrp4gy0zdZgdW6ZOqrAVoUYFz8JEKKtVBnCUxbSyRVjGZ8/Wjosf cBPoGL4cn30+FQn6Q9SFr4HWzxHl17xkrASDEqlhfYpqt0z8205DFOED8hRHvj3xnoMI 8nfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=cJ0QvUxo; 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 hk5-20020a17090b224500b001ca6bd88c6asi1524103pjb.63.2022.04.18.23.03.27; Mon, 18 Apr 2022 23:03:51 -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=cJ0QvUxo; 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 S243547AbiDRNKQ (ORCPT + 99 others); Mon, 18 Apr 2022 09:10:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59826 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241682AbiDRM65 (ORCPT ); Mon, 18 Apr 2022 08:58:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6EFB2DD79; Mon, 18 Apr 2022 05:39:27 -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 0FBF9B80EDB; Mon, 18 Apr 2022 12:39:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3AF86C385AC; Mon, 18 Apr 2022 12:39:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650285564; bh=brtuF43pM50XqWsrLtaTVNwuTiY5ZTnr6ySqkkguyiw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cJ0QvUxo0dGED9bEoAR7GvBWRCtgvtdgCmM0XWWe3vXA9v7pTLUJoLXJx47cyXuwR cTwxKP3OremulL0Jb52MmjLbSsnKnEse3pypodR61qPi1uUTos6FnXjp2/cJhtCL+N O2ZSePoGpk2zeFPrhi+ZnJcp1GQ9IcE6JmFLL2SY= 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 5.10 059/105] arm64: alternatives: mark patch_alternative() as `noinstr` Date: Mon, 18 Apr 2022 14:13:01 +0200 Message-Id: <20220418121148.162990767@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121145.140991388@linuxfoundation.org> References: <20220418121145.140991388@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 73039949b5ce..5f8e4c2df53c 100644 --- a/arch/arm64/kernel/alternative.c +++ b/arch/arm64/kernel/alternative.c @@ -41,7 +41,7 @@ bool alternative_is_applied(u16 cpufeature) /* * 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)); @@ -49,7 +49,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; @@ -94,7 +94,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