Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2232421rwb; Wed, 30 Nov 2022 04:16:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf5QRZ4puBroxykJZ7RfczH1/KpF1N923CUI1em6bR+CTdaD0nWY8SUDfb9M3S5UWYdNGPlo X-Received: by 2002:a17:906:c18c:b0:7b2:8a6e:c569 with SMTP id g12-20020a170906c18c00b007b28a6ec569mr52370370ejz.582.1669810585582; Wed, 30 Nov 2022 04:16:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1669810585; cv=none; d=google.com; s=arc-20160816; b=QZK+dGHdNXOfDyX+OMeE09IAqO5NT1gil960OR4a40MkHxiEjbimzUe5DLyeQhtpOl s71Ld3TYyYtZ+vHDSNghztGoMji4G9HWaed7/gQtMnisPzAT3vDPhYblwV4E+3mptmf1 RIVnNp93cbtHdx+KshmPGGpHBRSboSkDSTuyY/lyflex4OvSHj5HjM2ioRDbNSObzKIH NRilp9tvqjCX0GUR8BfMZQYQR7Q3v9f4wE0E2KKcmCZRvcsTB/X7qLgwd4mnzmHfxUKS 8su766BjL5zIEOJwXxKsLNxZbiLj9v+mNumIYYhYGqvgHLhxy/Z811Gv8yZM412DskSG U9sA== 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:references:in-reply-to:subject:cc:to:from :dkim-signature; bh=bO2gC3BeFa6eR0sZMkTL++rmJ+2QYw0mZSaqNk1jHEc=; b=WqOYCtQlvJ72FRrOt0pR1Myj0o+PB/gNdWQeA/XVWxIJYdGXrfQydwnHvZe86Kaa9i 6nLjbh9Bd/1Ng5eWQl5K+2OcKJGCFd2Jaz8OBfVAZNOsugbi/oexzcYaEntdZh4FPkVC 3nTWgqfXV4roLeAtQtiHCXqdKJNPTT4oHvB1nos1lvUQu3cyXqUm2ANpE7PhrNa5ODFG qAKAW/P79yHminDqzWtJqcSmBFWrjiigAYPx1gEIXXTNWLONkqNph952/2ej8Naa0tBk nBe7QnTTHicMslNHlnXR9rnWcbTCdHu2Cwj94WQLt9j+T2SXt77yBa542JvdLI0WZpG8 y3vw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=dYTi4FNx; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n24-20020a056402515800b004634dd8b6edsi1062340edd.296.2022.11.30.04.16.05; Wed, 30 Nov 2022 04:16:25 -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=dYTi4FNx; 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 S231274AbiK3LiU (ORCPT + 84 others); Wed, 30 Nov 2022 06:38:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229974AbiK3LiS (ORCPT ); Wed, 30 Nov 2022 06:38:18 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E93682C676; Wed, 30 Nov 2022 03:38:16 -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 ams.source.kernel.org (Postfix) with ESMTPS id A52CDB81AF2; Wed, 30 Nov 2022 11:38:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0CE6C433C1; Wed, 30 Nov 2022 11:38:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669808294; bh=5bEYlFEyZKCYZEyNNJPRBWYDQP0ERj+j46soRPrLxVs=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=dYTi4FNxDiF0nqG0Fg2vbGzAGx/ebqcpdavkWjmErzAeMgb6Dl9RLE1jKWnI1fHGg Cvbxb6NbVUL239BT6qpgAZ1vIcICYUW2GKqpKsXiTVSvZWoB+toUy+MwLym5aVTWk7 wFzDKjGt3rx/ZyVO/PlyPAhztE5YQ924dKxm2qz0RcvJN4CwXbwsSiYNFKoHcbUbhJ WXhetBwYCHdC+DdQH6qPtQ+FWgqhwscNcwT2TTo7O3nXoynM8kbTz4+q3rQtOrum4A coTd+zd4EXZC1Oi9jPc2ymUD3vWrIywn44pX9bnzUl3fGwGzmWTr2jMlLDhc6LHpi/ f+Z4r4BZuJlVQ== From: =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= To: Pu Lehui , bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , Paul Walmsley , Palmer Dabbelt , Albert Ou , Pu Lehui , Pu Lehui Subject: Re: [PATCH bpf] riscv, bpf: Emit fixed-length imm64 for BPF_PSEUDO_FUNC In-Reply-To: <20221130033806.2967822-1-pulehui@huaweicloud.com> References: <20221130033806.2967822-1-pulehui@huaweicloud.com> Date: Wed, 30 Nov 2022 12:38:11 +0100 Message-ID: <87h6yg1xlo.fsf@all.your.base.are.belong.to.us> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable 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 Pu Lehui writes: > From: Pu Lehui > > For BPF_PSEUDO_FUNC instruction, verifier will refill imm with > correct addresses of bpf_calls and then run last pass of JIT. > Since the emit_imm of RV64 is variable-length, which will emit > appropriate length instructions accorroding to the imm, it may > broke ctx->offset, and lead to unpredictable problem, such as > inaccurate jump. So let's fix it with fixed-length imm64 insns. Ah, nice one! So, the the invariant doesn't hold (the image grow in the last pass). > Fixes: 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper") This is odd? This can't be the right Fixes-tag... > Signed-off-by: Pu Lehui > --- > arch/riscv/net/bpf_jit_comp64.c | 31 ++++++++++++++++++++++++++++++- > 1 file changed, 30 insertions(+), 1 deletion(-) > > diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_com= p64.c > index eb99df41fa33..f984d5fa014b 100644 > --- a/arch/riscv/net/bpf_jit_comp64.c > +++ b/arch/riscv/net/bpf_jit_comp64.c > @@ -139,6 +139,30 @@ static bool in_auipc_jalr_range(s64 val) > val < ((1L << 31) - (1L << 11)); > } >=20=20 > +/* Emit fixed-length instructions for 32-bit imm */ > +static void emit_fixed_imm32(u8 rd, s32 val, struct rv_jit_context *ctx) > +{ > + s32 upper =3D (val + (1U << 11)) >> 12; > + s32 lower =3D ((val & 0xfff) << 20) >> 20; > + > + emit(rv_lui(rd, upper), ctx); > + emit(rv_addi(rd, rd, lower), ctx); > +} > + > +/* Emit fixed-length instructions for 64-bit imm */ > +static void emit_fixed_imm64(u8 rd, s64 val, struct rv_jit_context *ctx) > +{ > + /* Compensation for sign-extension of rv_addi */ > + s32 imm_hi =3D (val + (1U << 31)) >> 32; > + s32 imm_lo =3D val; > + > + emit_fixed_imm32(rd, imm_hi, ctx); > + emit_fixed_imm32(RV_REG_T1, imm_lo, ctx); > + emit(rv_slli(rd, rd, 32), ctx); > + emit(rv_add(rd, rd, RV_REG_T1), ctx); > +} Hmm, will this really be fixed? We can end up with compressed instructions, which can then be a non-compressed in the last pass, and we have the same problem? The range of valid address for RV64 (sv39 to sv57) are 0xffffffff00000000 to 0xffffffffffffffff, so I think we can do better than 6 insn, no? My gut feeling (I need to tinker a bit) is that 4 should be sufficient. Note that worst case for a imm64 load are 8 instructions, but this is not the general case. Bj=C3=B6rn