Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2555975rdb; Wed, 4 Oct 2023 05:07:47 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFMjQH4nC0bWrpy3Szbl6nv95Q7Xl9ff3++MJ2kw1+RSTujHAvjvnmPGoi135yKtFE+aqoa X-Received: by 2002:a05:6e02:1342:b0:351:4cdd:f50e with SMTP id k2-20020a056e02134200b003514cddf50emr1983914ilr.12.1696421267361; Wed, 04 Oct 2023 05:07:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696421267; cv=none; d=google.com; s=arc-20160816; b=qonFA0Tdj0Rty4cx12iMOt807AgoOGiYYm7jBuN1jLXYsTtIcd8UsFfmBKRzPXhGXK scOojinubqGMCpgigPGkOrcUD3VGUVAepthTXS3dt0uAuHnYj7rED4I9tSeGzPu9yOZX l51LD/YeeBY18Dg06BYK4HhDijHgTtHr4al9Nu/91D8DL9/Fxp82pcGDwwjvO1DLfDPX nMZrFtrCo507qGW6HK/qqa86+OD/SwNGSazPC/2Rs0Eg2WH4FD1qj2/ZfvFeIy64a6q5 cCWReRQsccx/9I6RR6yJFJO4Fn0n+5A75oVJlVq/auC7GTcDQmHBQlH9qei+H1YvggMX 6ubg== 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=BG6vFj8QsYhCMbKiAcCVm7xufDmqGGMcbr/U8OgbyXk=; fh=ITtiPTXeL1EZCuvub7OUZlLfMU6BhzN941tRZey3mqQ=; b=CzsTcmNsJcGSL2UWbcdHp0DGm78A4/blwHnJ/BtWURLEJghxeKss+1mqYWI/0wE7za iwDHggaqBygcSpkzEicjVp2vy8YLL3ZsWNrTU1HhNyx+825WE0QcEE+s6lJwO0TwJ12n J+ZUeZLXhD50mc2W3g+i6S7lL3FV8iyPKsJSSBvexGCNHXlt3r6tZTLZagvNpV1syZnB aDXZkNq4LqrX/d4sJoKL+rXBMSGrbJLQdgwvLDmNNEsAuGcxVmrFTuEZGLC7hfQ8Y/fg FRNnazgjlfrt70c3h4apgNKilNRMtMBo8YsEhRQvWIs9KPlwsR7Y0GxKFisvMehVhrjF n3pg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ffk7vBFT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id v6-20020a634646000000b005852a35aff4si3642779pgk.227.2023.10.04.05.07.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 05:07:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ffk7vBFT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 70E7881A8A9B; Wed, 4 Oct 2023 05:07:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242299AbjJDMHc (ORCPT + 99 others); Wed, 4 Oct 2023 08:07:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35978 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242218AbjJDMH2 (ORCPT ); Wed, 4 Oct 2023 08:07:28 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B6255F0 for ; Wed, 4 Oct 2023 05:07:24 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EE09AC433C9; Wed, 4 Oct 2023 12:07:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696421244; bh=vLvgsGAj3vc8B6/nSYNfFLo6uk+g4NpV7dEmI8AKTYI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ffk7vBFT5mbKfpS4JsRj1/srW+OQBeawmaCbwedd64L5PBIXa35ukJio0aGa/79fP XDBl5Dqtggjo+cBwBIOgZTuARLyykM12k507R6ubzMvE42nQ2mkhwyn5J80OENEIO8 m3Ag+25W74eAEFvJu1mfdI0GdG/B/l7fx9eOkhl+fRhEy9EzutvfDB7IuH3+cAM2Dp rUGeixzlVrVfIeeL3wZ6TSW0xUq9zLGPiBBeXGSRM+tE98wA3fqllIR+Q+IQHDCZ7A tCqaAqP3sb47Cf8hd462KfH4XTV8ZZxs1LSp6O2MSpCWgVNURx7xWbhuHNyFOqRwej CJSmc1nrCsyhQ== From: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , bpf@vger.kernel.org, netdev@vger.kernel.org, Pu Lehui Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , linux-kernel@vger.kernel.org, Luke Nelson , Xi Wang , linux-riscv@lists.infradead.org Subject: [PATCH bpf 2/2] riscv, bpf: Track both a0 (RISC-V ABI) and a5 (BPF) return values Date: Wed, 4 Oct 2023 14:07:06 +0200 Message-Id: <20231004120706.52848-3-bjorn@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231004120706.52848-1-bjorn@kernel.org> References: <20231004120706.52848-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, 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 pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Wed, 04 Oct 2023 05:07:42 -0700 (PDT) From: Björn Töpel The RISC-V BPF uses a5 for BPF return values, which are zero-extended, whereas the RISC-V ABI uses a0 which is sign-extended. In other words, a5 and a0 can differ, and are used in different context. The BPF trampoline are used for both BPF programs, and regular kernel functions. Make sure that the RISC-V BPF trampoline saves, and restores both a0 and a5. Fixes: 49b5e77ae3e2 ("riscv, bpf: Add bpf trampoline support for RV64") Signed-off-by: Björn Töpel --- arch/riscv/net/bpf_jit_comp64.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c index de4c9957d223..8581693e62d3 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -759,8 +759,10 @@ static int invoke_bpf_prog(struct bpf_tramp_link *l, int args_off, int retval_of if (ret) return ret; - if (save_ret) - emit_sd(RV_REG_FP, -retval_off, regmap[BPF_REG_0], ctx); + if (save_ret) { + emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx); + emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx); + } /* update branch with beqz */ if (ctx->insns) { @@ -853,7 +855,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET); if (save_ret) { - stack_size += 8; + stack_size += 16; /* Save both A5 (BPF R0) and A0 */ retval_off = stack_size; } @@ -957,6 +959,7 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, if (ret) goto out; emit_sd(RV_REG_FP, -retval_off, RV_REG_A0, ctx); + emit_sd(RV_REG_FP, -(retval_off - 8), regmap[BPF_REG_0], ctx); im->ip_after_call = ctx->insns + ctx->ninsns; /* 2 nops reserved for auipc+jalr pair */ emit(rv_nop(), ctx); @@ -988,8 +991,10 @@ static int __arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, if (flags & BPF_TRAMP_F_RESTORE_REGS) restore_args(nregs, args_off, ctx); - if (save_ret) + if (save_ret) { emit_ld(RV_REG_A0, -retval_off, RV_REG_FP, ctx); + emit_ld(regmap[BPF_REG_0], -(retval_off - 8), RV_REG_FP, ctx); + } emit_ld(RV_REG_S1, -sreg_off, RV_REG_FP, ctx); -- 2.39.2