From patchwork Tue Oct 15 11:39:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tiezhu Yang X-Patchwork-Id: 13836247 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 923281E9080; Tue, 15 Oct 2024 11:39:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728992364; cv=none; b=er2TnZBu9LpOOScJJbo6ohvEYvjWcNYk2scC9FDlRHa7MY4lvvEEzLL3FTyDjxISCS+6NcjhHLccUDkgLopuwVojBm6POu3/cGu//cNP0dwncA3KibUTkp82OVbUJrwNgfqZno9vJisZQJbaPwVKBE6kpMvqMa7JTS4xtPMyXsA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728992364; c=relaxed/simple; bh=rqu0fWfmQu2kYrqp0OPfNmQup6sQp3MP6/e6QsJ8MWc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GU6gguRw02MTGDVjbrNBRjYYsm4pHw/Z01uzjnEikg3Sc+6KTt4RyqScWeb47JIyna0+V/VJ85gqyosjz95eKn09ZSpJnrJ6rBRM2dsVDkP8v6PN519cvK9sC3vkPYDqYtONaKGbXV9dX1NGJHDf4OeyMkpx7hpEL45bT3SRlAI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Cx44hoVA5n938dAA--.42656S3; Tue, 15 Oct 2024 19:39:20 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front2 (Coremail) with SMTP id qciowMCxbcdkVA5n6E0uAA--.9583S6; Tue, 15 Oct 2024 19:39:19 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 4/6] bpf, core: Add weak arch_prepare_goto() Date: Tue, 15 Oct 2024 19:39:13 +0800 Message-ID: <20241015113915.12623-5-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241015113915.12623-1-yangtiezhu@loongson.cn> References: <20241015113915.12623-1-yangtiezhu@loongson.cn> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qciowMCxbcdkVA5n6E0uAA--.9583S6 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw4rWw1rKryfXF13uF17Jwc_yoW5AFW7pa s5u3yFkr4rXw4S934UJw4vgrWakr48KrZ8G3s5J34xAw4Ygw18ta4vgFnIyFZ8CrnYkr4I qw1UKryUKa4DAFgCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryUMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU8EeHDUUUUU== X-Patchwork-Delegate: bpf@iogearbox.net The objtool program needs to analysis the control flow of each object file generated by compiler toolchain, it needs to know all the locations that a branch instruction may jump into. In the past, objtool only works on x86, where objtool can find the relocation against the nearest instruction before the jump instruction, which points to the goto table, because there is only one table jump instruction even if there is more than one computed goto in a function such as ___bpf_prog_run(). In fact, the compiler behaviors are different for various archs. On RISC machines (for example LoongArch) this approach does not work: with -fsection-anchors (often enabled at -O1 or above) the relocation entry may actually points to the section anchor instead of the table. Furthermore, objdump kernel/bpf/core.o shows that there are many table jump instructions in ___bpf_prog_run() with more than one computed gotos, but there are no relocations which actually points to the table for some table jump instructions on LoongArch. For the jump table of switch cases, a GCC patch "LoongArch: Add support to annotate tablejump" has been merged into the upstream mainline, it makes life much easier with the additional section ".discard.tablejump_annotate" which stores the jump info as pairs of addresses, each pair contains the address of jump instruction and the address of jump table. For the jump table of computed gotos, it is indeed not so easy to implement in the compiler, especially if there is more than one computed goto in a function. Without the help of compiler, in order to figure out the address of goto table by interpreting the LoongArch machine code, add a function arch_prepare_goto() for goto table, it is an empty weak definition and is only overridden by archs that have special requirements. This is preparation for later patch on LoongArch, there is no any effect for the other archs with this patch. Suggested-by: Xi Ruoyao Signed-off-by: Tiezhu Yang --- kernel/bpf/core.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 5e77c58e0601..81e5d42619d5 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1706,6 +1706,14 @@ bool bpf_opcode_in_insntable(u8 code) } #ifndef CONFIG_BPF_JIT_ALWAYS_ON +/* + * This symbol is an empty weak definition and is only overridden + * by archs that have special requirements. + */ +#ifndef arch_prepare_goto +#define arch_prepare_goto() +#endif + /** * ___bpf_prog_run - run eBPF program on a given context * @regs: is the array of MAX_BPF_EXT_REG eBPF pseudo-registers @@ -1743,6 +1751,7 @@ static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn) #define CONT_JMP ({ insn++; goto select_insn; }) select_insn: + arch_prepare_goto(); goto *jumptable[insn->code]; /* Explicitly mask the register-based shift amounts with 63 or 31