From patchwork Mon Feb 6 09:04:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guo Ren X-Patchwork-Id: 53091 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2129415wrn; Mon, 6 Feb 2023 01:06:53 -0800 (PST) X-Google-Smtp-Source: AK7set85sKyuFCzu+8TvzQUF8pSifJDNb8cJkGgSOlqx3QxvHBeTMD7qhwbCqlc/AisquUIQTULq X-Received: by 2002:a05:6a20:c50f:b0:be:bfdc:621a with SMTP id gm15-20020a056a20c50f00b000bebfdc621amr18764526pzb.30.1675674412993; Mon, 06 Feb 2023 01:06:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675674412; cv=none; d=google.com; s=arc-20160816; b=s8XSgsp/EzJoxM9orGGgRIEnaCosDEEakRbkavkPs7mbDfwY3Sspp/t7Pv3siue3T4 wpLBTWeKtkjBajIs4K4GtMWrEOEF1r2tRPWmhYyQ6WNf+mrCDSnwDRzo7ga7HvK4T6VW Ii8pVCiQEeZ5Yij2lzoHEZlrlhOsSczIdOo7LqTqDc2pYnRYtiMtTbQehjpGL2XLBWVw g1L1YxKjFdqmNY7attXnoQkHKk5eWs55v/VyKcFTGBgB+KsAlZy9YfI/jIoG3qAf7ghz ehincrG4s+gxxZoC8OUgDAqLG63M5xGftcJGBk22CtrB5YOjYMJnweWc4sPKphUMYltn vzCg== 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 :message-id:date:subject:cc:to:from:dkim-signature; bh=lkWAk9rdsrAyN7I5suRwE8GXftcfxo7aKNJenKvd/Rg=; b=TkbygUMyK/dO8bm/Pq9GTlquuky5jgUmPYiPkwigRpZgZadCmNg0UkeJjEwnFcH0/E sda2uRIhY+zK3DRvJ5td9bx4GXGMY8cHK8tsxGuDuHI9vULb07Ac+Sb111HPjuXHCnU1 McibGI7K5mg0CKVEVEFAQOF4s6eAykdQvq4JkTPQauf5Mb+vXc51l3v+SIW4UKnigDgS 97OoU5esEWzbqeYhVVlP/O5DeUJKDS7KZRXwnjZzcFaEXY3PBy6ve2PYrLiPo39LXsK5 tJsrsTNEyY/OgcK18oLXay2+vwksWvs6XyxX/5uK0KEdPaWFmugJpv47trLj3CjSMtCZ O/EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=aFS05FMG; 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=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l192-20020a6391c9000000b004faaa3461cdsi7380700pge.783.2023.02.06.01.06.37; Mon, 06 Feb 2023 01:06:52 -0800 (PST) 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=@kernel.org header.s=k20201202 header.b=aFS05FMG; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229645AbjBFJEy (ORCPT + 99 others); Mon, 6 Feb 2023 04:04:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229661AbjBFJEx (ORCPT ); Mon, 6 Feb 2023 04:04:53 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67A8AEC76 for ; Mon, 6 Feb 2023 01:04:52 -0800 (PST) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 9F44660DBB for ; Mon, 6 Feb 2023 09:04:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1236EC433EF; Mon, 6 Feb 2023 09:04:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1675674291; bh=Ip3VDTZXdwcZk64rGGwgeWdelFU51lDl/YMAUsnvUpg=; h=From:To:Cc:Subject:Date:From; b=aFS05FMGfh3qQ5LM4bNqbd67zVrlY9Upy/BEiDO2/9TdeEG1XziYDqzA6e3OIVCtI yS+dMQqizHqN/IMeLsT/kfgM7VQsvkE2KcmqVzT8vBJ+PTRKX7mn/JYLcUW4ebCMow TYuy1sgdq1zcqotD/ccoQgomQ064AqgHd6bza4RZ1T8xMidmOsVVv6JgD4EkTku/JI +OqdOES1nP1YcxQ8bqNO1lFa7amS8CP6+cWasE2NkcJOz+mtWyH1DhWv9ENTu0VT6m sVvKhLmZMTsWXenpHuNSMqhjkmr0hv8YOOVShfy7KgCIvIoDs173YqNWfOBKDipMtN cOPu93je5gDzA== From: guoren@kernel.org To: guoren@kernel.org, palmer@dabbelt.com, paul.walmsley@sifive.com, mhiramat@kernel.org, conor.dooley@microchip.com, penberg@kernel.org, mark.rutland@arm.com, jrtc27@jrtc27.com, andy.chiu@sifive.com, zong.li@sifive.com, greentime.hu@sifive.com, bjorn@kernel.org Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH RESEND] riscv: jump_label: Fixup unaligned arch_static_branch function Date: Mon, 6 Feb 2023 04:04:40 -0500 Message-Id: <20230206090440.1255001-1-guoren@kernel.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757071972898796748?= X-GMAIL-MSGID: =?utf-8?q?1757071972898796748?= From: Andy Chiu Runtime code patching must be done at a naturally aligned address, or we may execute on a partial instruction. We have encountered problems traced back to static jump functions during the test. We switched the tracer randomly for every 1~5 seconds on a dual-core QEMU setup and found the kernel sucking at a static branch where it jumps to itself. The reason is that the static branch was 2-byte but not 4-byte aligned. Then, the kernel would patch the instruction, either J or NOP, with two half-word stores if the machine does not have efficient unaligned accesses. Thus, moments exist where half of the NOP mixes with the other half of the J when transitioning the branch. In our particular case, on a little-endian machine, the upper half of the NOP was mixed with the lower part of the J when enabling the branch, resulting in a jump that jumped to itself. Conversely, it would result in a HINT instruction when disabling the branch, but it might not be observable. ARM64 does not have this problem since all instructions must be 4-byte aligned. Fixes: ebc00dde8a97 ("riscv: Add jump-label implementation") Link: https://lore.kernel.org/linux-riscv/20220913094252.3555240-6-andy.chiu@sifive.com/ Reviewed-by: Greentime Hu Signed-off-by: Andy Chiu Signed-off-by: Guo Ren --- This patch is independent from: https://lore.kernel.org/linux-riscv/87pmangqpt.fsf@all.your.base.are.belong.to.us/ --- arch/riscv/include/asm/jump_label.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/riscv/include/asm/jump_label.h b/arch/riscv/include/asm/jump_label.h index 6d58bbb5da46..14a5ea8d8ef0 100644 --- a/arch/riscv/include/asm/jump_label.h +++ b/arch/riscv/include/asm/jump_label.h @@ -18,6 +18,7 @@ static __always_inline bool arch_static_branch(struct static_key * const key, const bool branch) { asm_volatile_goto( + " .align 2 \n\t" " .option push \n\t" " .option norelax \n\t" " .option norvc \n\t" @@ -39,6 +40,7 @@ static __always_inline bool arch_static_branch_jump(struct static_key * const ke const bool branch) { asm_volatile_goto( + " .align 2 \n\t" " .option push \n\t" " .option norelax \n\t" " .option norvc \n\t"