From patchwork Thu Jan 26 17:06:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Guo Ren X-Patchwork-Id: 48808 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp382965wrn; Thu, 26 Jan 2023 09:08:42 -0800 (PST) X-Google-Smtp-Source: AK7set+21s5Ij0FKBdrNU8FtscD4iUl+uqBCOoUfPycZ9Il7pRZe/gbWj/oF+7VrCYVp2Z7ufFkJ X-Received: by 2002:a17:903:191:b0:196:2364:e908 with SMTP id z17-20020a170903019100b001962364e908mr9036785plg.37.1674752921554; Thu, 26 Jan 2023 09:08:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674752921; cv=none; d=google.com; s=arc-20160816; b=gaF+DmJs0Gk/aQDTvgof35umSE3GUO256YAyn23elM/0KtPbLI0N7d8urffcbeXCI0 5j0Egrn6fuleoeGUWgZMoijrkMsDHyqe+DD+r2KaanvNlSNJPWy/1Nv1Vo5H9fPqDoxB hHIS5mxHLzbqCbtQm1Ika1oFfWP0+DZP1/gtcfs69TD/d/RLJLJ00HcTik4QEeIB2oDJ yHU/XUlJf04lxAh2IhPl8KXKBaStkV1qqj8OGaWPnlw4akP2NBgzWzlttj88AAzo/kwa 1QW/t/jLpgCxYMY0Rtx1kkAx+nnikeHkyzZNjRt7FUwCYUAQQVS/fuq46cqcgS1bir3A IlyA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Z2cgUg6Z8WvOq7VJKddMiFCo7SwZD9KTo1FfPkrqmd4=; b=G0vHSC2az37qhnQPTjyKq9Y05s/hKcSAO6M5Zdw1ZLSjn26OAD79RsONLofSPixyNs JTel3JdoK8X9Fsf6psBpw9RkSstKM+e2P5v2mIMI0qy5a/WzLI3Gbe6MnmpACly/AqI6 CM7dOfNSWAiXkcigKyBoo0BcPPxBf33GLwhljxAGPv1/LJM6yqHLmjHwwskRxsNyLttA o25JxtSrqHJaoN0unDpRRWFslv74koSsVSgs9wTia07BtCmUaYEUp/XkNHzjbaWNNciO AXf2zuXjNPEDxZE4H0FLgu33K+Sh8I2BYDVY+sCS8qgPP5pQoz6c/NqphPW0TiFeKaeH 2cPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="rb/s6IV3"; 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 d16-20020a170902aa9000b00186bb39ff55si2005404plr.427.2023.01.26.09.08.28; Thu, 26 Jan 2023 09:08:41 -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="rb/s6IV3"; 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 S231849AbjAZRGa (ORCPT + 99 others); Thu, 26 Jan 2023 12:06:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44058 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231876AbjAZRG3 (ORCPT ); Thu, 26 Jan 2023 12:06:29 -0500 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 204842F7BD for ; Thu, 26 Jan 2023 09:06:25 -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 sin.source.kernel.org (Postfix) with ESMTPS id 02E63CE2386 for ; Thu, 26 Jan 2023 17:06:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7439AC4339B; Thu, 26 Jan 2023 17:06:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674752782; bh=ZvStsc5ZtEWBoYSz9j8G0L0vCieAFt37Hxf2WaPKWWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rb/s6IV3YcxZ4VRJQvCzKHhmseUDBTzSjrE4vMAt2T4RfuOHyH99josgXNwDAZjk4 1n5ppRrKyBdEeLEItArSfRfL6vt0zUqPslv/sjyaNQ3aZErHap+aFy6ZdotoNNvoat QwlhZeArvCbJIUdm73XaXSsSQhDQb3MRgN6kcBHAtHRPvIyGr1c2DnKfQaO80IVr+L j/OiOfCE8Ot4T9ULk2AvtHmiLQIFvtCiUuBV5Hg1G/V0GKIucx7skHdI7+9cnVwuuy ECWwGY51kbg8gm6e1YjyOi5bALvymqIfMByitwME+oq9LKdcrCw4ONNLkt69NeB62n qWp/MSASdtLdw== 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 Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Greentime Hu Subject: [PATCH -next V3 1/2] riscv: jump_label: Fixup unaligned arch_static_branch function Date: Thu, 26 Jan 2023 12:06:06 -0500 Message-Id: <20230126170607.1489141-2-guoren@kernel.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20230126170607.1489141-1-guoren@kernel.org> References: <20230126170607.1489141-1-guoren@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 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 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?1756105719417165621?= X-GMAIL-MSGID: =?utf-8?q?1756105719417165621?= 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/ Signed-off-by: Andy Chiu Reviewed-by: Greentime Hu Signed-off-by: Guo Ren Reviewed-by: Björn Töpel --- 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" From patchwork Thu Jan 26 17:06:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guo Ren X-Patchwork-Id: 48809 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp383005wrn; Thu, 26 Jan 2023 09:08:45 -0800 (PST) X-Google-Smtp-Source: AMrXdXsh72ysrKyFCXkWsU+tXI0Tpqx03cSREzg95ZyYJpX3/KpssWZKOq2U6TO10N331tm7PKuF X-Received: by 2002:a17:90b:3eca:b0:22b:b89b:b9d0 with SMTP id rm10-20020a17090b3eca00b0022bb89bb9d0mr23389989pjb.22.1674752924675; Thu, 26 Jan 2023 09:08:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674752924; cv=none; d=google.com; s=arc-20160816; b=bPWBpe37XuTVWsHUSK7Dw3LOEr4b4Ujuvd1bRh7+ZwYVQFrdGe1vdyikWTsTeyjMS0 5/pCoDtO7qTs/gu+cR8u3DlW4LoFAjZsZPrdXA4o8Tl8/MVsFk52kXw1QNjaJQCaLRrL O1fT67q9YO39xt92O4zAURSpO1vhEx1if8/wot1jRxRChcl8VnmMSO1VMT8sx9LGtTsz i0qO7qk973pLLEq6CPSfxL8alpEwlNwRqPgNedazwltrpE7LKNhpTgiyEfxtcieTW2y1 9eDvFmd0leEn79C8Ar0UsMw0dwdyINQYb/PRmL5foVAD3SFT/ylX4WK1oFvvWEuXLImn LOFg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Udzi6lFT0hr2p+ptjGK5Fe/3p1MUudCMBctlNVKluUE=; b=aviRxbfGjxhYh5PxnaMW6zQA195P5fddu+AQHx6MOO3KoRaZ6wpJ1asCWPzdeq+15Z o4qu33E4A9laqeRMuqRxVdaZKyoLOyIGIRphD6y7RKPXERZOL4IQbRaMssnYkrNYTmSE wiYT7BwOVJfkQ2VtPxYahsaLRyK0lEBnYq9WqqiGLwr0+Ksbr9tnQWSscHCBQ+VuQS2f IbiXlhL5RVPsMLaQT1eAK4a7zlgJGPtJWfjbjDWRJR8yD8u2RB8GAoKRzMHO2uyzwa6v UvQ/GxAz4R8yTfWG3/E7N9BvbSqfmKa2TVneDLjf92WSfB9fPn3TPjkPSLOpDeg4D9Yj KRAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Z8vg+Tjd; 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 bj7-20020a17090b088700b0022bf0841a2csi5360038pjb.180.2023.01.26.09.08.33; Thu, 26 Jan 2023 09:08:44 -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=Z8vg+Tjd; 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 S231939AbjAZRGc (ORCPT + 99 others); Thu, 26 Jan 2023 12:06:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231918AbjAZRGa (ORCPT ); Thu, 26 Jan 2023 12:06:30 -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 0B63FA6 for ; Thu, 26 Jan 2023 09:06:27 -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 4B3AC61900 for ; Thu, 26 Jan 2023 17:06:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E2676C433EF; Thu, 26 Jan 2023 17:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1674752786; bh=TJzRlX8qvgEiSePWc4rQnWbXyiOPKt8ROYEh5q9ZOX8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z8vg+Tjd1ExZ+O8g5stUbVMTPL70WkZNIh5WTjGx/wNXUurHpYJxFyvedy0VT6PZU KcV4PrvbOaJlt6BlcQzOBXWZsuXOrqguvA4m8l5U2izzrtcVrE8TGpcMEwwclC1yT8 e2mumldyttgfH48A1u0b+qIjOFpckm4UX9/es822Xv2A2xGVwv9x0LeewO8Wxi6Zn0 9oMuis5wzFwJdMzmZJTaguSb0m/kGVhILRDnbbVE9fVdl6spkSxY0qT6ofzDdqRVfh cn4Yq4X2NFUAOAK4OAXwHcb1JZybzcYzrEvRq1JCgg379+9+kQU9/RGSaOnQI8mEz7 M2ARWsvJzjJKQ== 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 Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Guo Ren Subject: [PATCH -next V3 2/2] riscv: jump_label: Optimize the code size with compressed instruction Date: Thu, 26 Jan 2023 12:06:07 -0500 Message-Id: <20230126170607.1489141-3-guoren@kernel.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20230126170607.1489141-1-guoren@kernel.org> References: <20230126170607.1489141-1-guoren@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 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 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?1756105722676963174?= X-GMAIL-MSGID: =?utf-8?q?1756105722676963174?= From: Guo Ren Reduce the size of the static branch instruction and prevent atomic update problems when CONFIG_RISCV_ISA_C=y. It also reduces the jump range from 1MB to 4KB, but 4KB is enough for the current riscv requirement. Signed-off-by: Guo Ren Signed-off-by: Guo Ren --- arch/riscv/include/asm/jump_label.h | 16 +++++++++++---- arch/riscv/kernel/jump_label.c | 30 +++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/arch/riscv/include/asm/jump_label.h b/arch/riscv/include/asm/jump_label.h index 14a5ea8d8ef0..afc58c31d02b 100644 --- a/arch/riscv/include/asm/jump_label.h +++ b/arch/riscv/include/asm/jump_label.h @@ -12,17 +12,23 @@ #include #include +#ifdef CONFIG_RISCV_ISA_C +#define JUMP_LABEL_NOP_SIZE 2 +#else #define JUMP_LABEL_NOP_SIZE 4 +#endif 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" +#ifdef CONFIG_RISCV_ISA_C + "1: c.nop \n\t" +#else "1: nop \n\t" +#endif " .option pop \n\t" " .pushsection __jump_table, \"aw\" \n\t" " .align " RISCV_LGPTR " \n\t" @@ -40,11 +46,13 @@ 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" +#ifdef CONFIG_RISCV_ISA_C + "1: c.j %l[label] \n\t" +#else "1: jal zero, %l[label] \n\t" +#endif " .option pop \n\t" " .pushsection __jump_table, \"aw\" \n\t" " .align " RISCV_LGPTR " \n\t" diff --git a/arch/riscv/kernel/jump_label.c b/arch/riscv/kernel/jump_label.c index e6694759dbd0..08f42c49e3a0 100644 --- a/arch/riscv/kernel/jump_label.c +++ b/arch/riscv/kernel/jump_label.c @@ -11,26 +11,52 @@ #include #include +#ifdef CONFIG_RISCV_ISA_C +#define RISCV_INSN_NOP 0x0001U +#define RISCV_INSN_C_J 0xa001U +#else #define RISCV_INSN_NOP 0x00000013U #define RISCV_INSN_JAL 0x0000006fU +#endif void arch_jump_label_transform(struct jump_entry *entry, enum jump_label_type type) { void *addr = (void *)jump_entry_code(entry); +#ifdef CONFIG_RISCV_ISA_C + u16 insn; +#else u32 insn; +#endif if (type == JUMP_LABEL_JMP) { long offset = jump_entry_target(entry) - jump_entry_code(entry); - - if (WARN_ON(offset & 1 || offset < -524288 || offset >= 524288)) + if (WARN_ON(offset & 1 || offset < -2048 || offset >= 2048)) return; +#ifdef CONFIG_RISCV_ISA_C + /* + * 001 | imm[11|4|9:8|10|6|7|3:1|5] 01 - C.J + */ + insn = RISCV_INSN_C_J | + (((u16)offset & GENMASK(5, 5)) >> (5 - 2)) | + (((u16)offset & GENMASK(3, 1)) << (3 - 1)) | + (((u16)offset & GENMASK(7, 7)) >> (7 - 6)) | + (((u16)offset & GENMASK(6, 6)) << (7 - 6)) | + (((u16)offset & GENMASK(10, 10)) >> (10 - 8)) | + (((u16)offset & GENMASK(9, 8)) << (9 - 8)) | + (((u16)offset & GENMASK(4, 4)) << (11 - 4)) | + (((u16)offset & GENMASK(11, 11)) << (12 - 11)); +#else + /* + * imm[20|10:1|11|19:12] | rd | 1101111 - JAL + */ insn = RISCV_INSN_JAL | (((u32)offset & GENMASK(19, 12)) << (12 - 12)) | (((u32)offset & GENMASK(11, 11)) << (20 - 11)) | (((u32)offset & GENMASK(10, 1)) << (21 - 1)) | (((u32)offset & GENMASK(20, 20)) << (31 - 20)); +#endif } else { insn = RISCV_INSN_NOP; }