From patchwork Fri Mar 24 08:23:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guo Ren X-Patchwork-Id: 74420 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp460408vqo; Fri, 24 Mar 2023 01:26:09 -0700 (PDT) X-Google-Smtp-Source: AKy350bjCOyLV94EPN9kQVyhGQ5migzwLpUuxXTENi5NaQDtZMawPJEA8nYQlupFfwAOP26EJMiN X-Received: by 2002:aa7:db96:0:b0:4fd:2ad2:13c4 with SMTP id u22-20020aa7db96000000b004fd2ad213c4mr2228690edt.21.1679646369324; Fri, 24 Mar 2023 01:26:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679646369; cv=none; d=google.com; s=arc-20160816; b=EfswGd6cD3WDdOumyhTRfUhjX7ITd9lmt6bL+Cvla0Qk0BaXK3VUL2D1WrARCOz9xK E9JhdL0qrkEsuB180VWKddhzCd9ZlDJv3uIt1f85nd0LEjk9eAGCbwtZ7FNSgDIF5gVC X1Xf/gBBsiEALcHK+x8WUOlfoWWKAQdjuxioeMPAK3fwY24PNKu4gUn2kSk4ytj3S8TE oW959y7UA4gKZLBHpmn91SoxtXrlL+ecCKfFmnyH3/SjBoiiwcBrvu14G716N7hhrbmR HGjuU0+El/V1BaMUIjqF3AZAnBkQkYvfFCHOigIuf5n16oL7yS3uKAp0ZhSJuP1rMj2X NoMA== 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=jmloGdsauTEtrF9MvADVpM8R5Xu15sIDqgaU1o57v7Q=; b=nXNh7+XAua0C6UtjPdhn4wggLG1XDFMjhU+SYf6Kwtf1FecLsPC8kyc08UjXzN1j2w nhQ+3XXGHBQenoKp5wTv9P9tj7rO0iFvG9MJpnjb6xfolzLc9vf0S904JM/qJiLXgjp6 nhyjTeSOFERMtdfTvWP9oLWonXtWVq4ivs9RrBkKJRY4e9VRpIoNZ/Pyu357kCiRwXjo QC2/xebG077TUd9dQrcTFtL8UiRnpZlqtaIl0vtns5nezvj1aOslY7YafET2CUin+W+T 9Qkgt+moRv464nmEw9gsrErqhBqwyPGFS9++gu43k0yUmiO/HisIUzAN1ffpKHXcQIjf A5Zw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dDeq+INQ; 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 n13-20020aa7d04d000000b00501c4e46efasi13352356edo.345.2023.03.24.01.25.45; Fri, 24 Mar 2023 01:26:09 -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=@kernel.org header.s=k20201202 header.b=dDeq+INQ; 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 S230350AbjCXIXx (ORCPT + 99 others); Fri, 24 Mar 2023 04:23:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229484AbjCXIXw (ORCPT ); Fri, 24 Mar 2023 04:23:52 -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 018CBE057; Fri, 24 Mar 2023 01:23:50 -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 3D54FB81E21; Fri, 24 Mar 2023 08:23:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43031C433EF; Fri, 24 Mar 2023 08:23:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1679646228; bh=WNmnBMlXQbbzSIzNbiTJv8PajglyS7jPcj9FFmg6NWU=; h=From:To:Cc:Subject:Date:From; b=dDeq+INQJQ8cdGwiqzVv1YUb+oIdwqPJJHuTj1Q0fbnl92ex0saerlTqYpdWYHewN 7CF1K1iztsPDj71V0OW3fxLF/4LCJVPqvt1gitdHYBWfUfRHssbptB7A0tdQN3VG6p 3xDGQe+GA3LdUHIFx+Yh5EihmAcO7RdA9gqiVoHQIjfB/MENJKU090orHMCrRU++Ks thLUVJeZlY9O/HNWXo5LuNn22vCOzw1j7jatENm+YJDEV2xili6xuUmTDnbS8Ud/6J USGAGp4AH+yBsBdNwBnvZlFvpNR/J5aEu53kvZ0Aijlxp8iO7UY0E2bSauakE6Xcow H9Ady5C0BXUpQ== From: guoren@kernel.org To: arnd@arndb.de, guoren@kernel.org, palmer@rivosinc.com, conor.dooley@microchip.com, heiko@sntech.de, jszhang@kernel.org, bjorn@kernel.org Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, Guo Ren Subject: [PATCH -next V4] riscv: jump_label: Optimize the code size with compressed instruction Date: Fri, 24 Mar 2023 04:23:20 -0400 Message-Id: <20230324082320.290410-1-guoren@kernel.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS autolearn=unavailable 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?1761236871375885197?= X-GMAIL-MSGID: =?utf-8?q?1761236871375885197?= 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 --- Changelog v4: - Rebase on palmer/for-next (20230324) - Separate from "riscv: jump_label: Fixup & Optimization" v3: https://lore.kernel.org/linux-riscv/20230126170607.1489141-3-guoren@kernel.org/ v2: https://lore.kernel.org/linux-riscv/20221210100927.835145-3-guoren@kernel.org/ v1: https://lore.kernel.org/linux-riscv/20220913094252.3555240-6-andy.chiu@sifive.com/ --- 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; }