Message ID | ed7920365daf5eff1c82892b57e918d3db786ac7.1701193577.git.dxu@dxuuu.xyz |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp4111694vqx; Tue, 28 Nov 2023 09:56:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IF1y9+Fpv+gJc+hpHvj67s2hnbCT547FcHuAt1R9pb9xjtW/R4nnoHRbup8OjxbGQ6frbwT X-Received: by 2002:a05:6a00:3926:b0:68f:a92a:8509 with SMTP id fh38-20020a056a00392600b0068fa92a8509mr30465902pfb.7.1701194190085; Tue, 28 Nov 2023 09:56:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701194190; cv=none; d=google.com; s=arc-20160816; b=lQlyS/RQuEAm+3uIkyOxqWooEVE//mmIYmcDtfmopJGftHX1FEWsuu9tgPMpw3/Jwg KKs+acLULsP4YRfP3zr/6zxvAOV04zg/hrWjrvgPkysnpUThuuPdm9k0+rats0ez69yw n0SO4Kl+lrHugErfxS88XGGvdbn6zcWTeVNDRxHn6+DFhyq1BeeYDWkorZrBImwiEmkO LGjmQ1Sm6vJ3pMrGXtMrg9GgZC5/m5Xpl6Xb7nnQXWYqk+AaFVgRIsc0gjXliNBR1IDJ twY+bX1s5yeQbXtM4o1XRoienoMdgwkAwg6loZ/6kaXxdHfVqGUhh4RNXelBy45yABrH FjsQ== 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 :feedback-id:dkim-signature:dkim-signature; bh=dglH5gXWrAma2xG/u6UyN2gBVthWUPYa50A8Hb6InUE=; fh=0HAHtITq3ywsUkKweeSnLGMdlzfcfzek5mSjqVFpvm0=; b=i2fwFqmjGG93tOnwUg+68KyiaaFtcMXpBEVd3y/WfnltIqTbPN6M4u6oJHj4nCyq3O npE9WM3ZhXyp2uZmBvkj4WdS5I/TKXpXoGmRg5iUhWeP7j/kEGnDUiH2a1WY8ccQExgT drR9PuPbuglRl98USdCHVD4ePUchvEIyZKJ8Cgw3T44Q0Cjht0aKOkmCQVqqrn/UH+7s tS+ogV26nQ1g8nKc9vMPaWkfgbfJpzQgIEWVZv85e7VMwIXgjZ/8FTjc1c0KawZR3rZ3 fa9rkpP8Wgk3fQonL4rEE03LjqvARieL7CjNfpUTL+vKIFAcw8NgY8d3O2/GHBzurALc tbQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=Wufyli4R; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=y5wRvLz4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id fi37-20020a056a0039a500b006cb69513cc5si13030191pfb.254.2023.11.28.09.56.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Nov 2023 09:56:30 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=Wufyli4R; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=y5wRvLz4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 57FE5804C1B8; Tue, 28 Nov 2023 09:56:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346700AbjK1Rz2 (ORCPT <rfc822;toshivichauhan@gmail.com> + 99 others); Tue, 28 Nov 2023 12:55:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346718AbjK1Ry5 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Tue, 28 Nov 2023 12:54:57 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5D33F19A9; Tue, 28 Nov 2023 09:54:56 -0800 (PST) Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailnew.nyi.internal (Postfix) with ESMTP id 84CC8580546; Tue, 28 Nov 2023 12:54:55 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Tue, 28 Nov 2023 12:54:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1701194095; x= 1701201295; bh=dglH5gXWrAma2xG/u6UyN2gBVthWUPYa50A8Hb6InUE=; b=W ufyli4REJ6JnBRtaCU0Pz86CKsnf932Pu+Z1cKsmfMjG+Ci4d7bs9UUBeQFI2a3Z 8XUw42JDgjOHCUPM3IqNgQMLNWZZBE5mt7sCPf5c/ysdk++c7IoenDZg4nBbOHgK b9IZnrV6XiK0nqYbArcBNb84FCOoqEtRH0UyQl56be+MlJddD5cNNA3uURFbIVQ9 K5KOpbQzaNS7V84uasdXFWMEdArjFCSVP7ZNZ3te7gFsthpvlohNiLJLLqAgZhF5 KUceYnLFE63tbc7oQuSWRgc8+PLMOovz3AwuVJzUPYagEBjCQ86VfUz3SMNvKPpm ncjiXkY5X+M9c8irtsWsA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1701194095; x= 1701201295; bh=dglH5gXWrAma2xG/u6UyN2gBVthWUPYa50A8Hb6InUE=; b=y 5wRvLz4my7qdH4DHDizhlFoKpOIq6VA5lVQT2dAdB9OCLcoHpLR2/h0Ya2qG5SXF lrr7hXSr1D61GEM6vFPQYc55luZEnd/oNsvWfA6lBCoTuSinKaDEhPF7E3VvImCG t6exMEvJQ3gChzFrKv9T6gjnoqsnCCFTjk7Q+eVc7xINCj4UrYNRrvqhVjrPJxQb GQr6pK+b45s7QtsFOCBOeVBSkDUZou2MeHBjateUWZuvq6JFP+GyeAGpl974niC/ AfCOMPr2zLvHhDZKlvRYJrshrkjlk3DyUJMn8Im9dU7FDs7CAHXkeEJUkMo8D4PW BVQwi6K/oLv8bMcVsxPng== X-ME-Sender: <xms:bylmZWO4XUb9EqEM6kK8jyLoBBMWTq5gDHokmdt7hRMZDiDnJd-v9A> <xme:bylmZU-QykbjCCqzLdHG23jCSf3ylOUY8PtbySJa4j30eZHmyhjBFxToH6rhANTHs jGmldZoGmRuWjUJ1Q> X-ME-Received: <xmr:bylmZdTDGqQivf7yrOZDNNv2HTLOCBS9VyVVUwlm7Rd5M0V72OLDfYafMtxCbYktMIFeRI39Zz36nptph-Ruqkwm2w2r5fBpykwFSWBeLhvmAQ> X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeifedguddthecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculddvfedmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpeeigeffteehteffhe ejkeefjeeuudfgvdekkeetudeghedugffgleffhefgjeevgfenucffohhmrghinheplhhl vhhmrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrh homhepugiguhesugiguhhuuhdrgiihii X-ME-Proxy: <xmx:bylmZWtFqY_DIcSfbFUCTdRsfZhPtVOkkehK_kjwG5GUiD4K65Cl2A> <xmx:bylmZed0WETNxDW3GiDSQZmCAZbiA6Hk64YnwvkRrqha2IVhKQ_APA> <xmx:bylmZa3siyrx_9Djkorp1ValDwlR-pkwuJWvHBND0_0U_APEgit68Q> <xmx:bylmZaFLmtqr7-sonV0zW190AeyfNWycjj9dIAL-ZnKr-gGpSUvKEA> Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 28 Nov 2023 12:54:53 -0500 (EST) From: Daniel Xu <dxu@dxuuu.xyz> To: ndesaulniers@google.com, andrii@kernel.org, nathan@kernel.org, daniel@iogearbox.net, ast@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com Cc: martin.lau@linux.dev, song@kernel.org, john.fastabend@gmail.com, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, trix@redhat.com, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, devel@linux-ipsec.org, netdev@vger.kernel.org Subject: [PATCH ipsec-next v2 3/6] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro Date: Tue, 28 Nov 2023 10:54:23 -0700 Message-ID: <ed7920365daf5eff1c82892b57e918d3db786ac7.1701193577.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.1 In-Reply-To: <cover.1701193577.git.dxu@dxuuu.xyz> References: <cover.1701193577.git.dxu@dxuuu.xyz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 28 Nov 2023 09:56:24 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783831399014738219 X-GMAIL-MSGID: 1783831399014738219 |
Series |
Add bpf_xdp_get_xfrm_state() kfunc
|
|
Commit Message
Daniel Xu
Nov. 28, 2023, 5:54 p.m. UTC
Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield writing wrapper to make the verifier happy. Two alternatives to this approach are: 1. Use the upcoming `preserve_static_offset` [0] attribute to disable CO-RE on specific structs. 2. Use broader byte-sized writes to write to bitfields. (1) is a bit a bit hard to use. It requires specific and not-very-obvious annotations to bpftool generated vmlinux.h. It's also not generally available in released LLVM versions yet. (2) makes the code quite hard to read and write. And especially if BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to to have an inverse helper for writing. [0]: https://reviews.llvm.org/D133361 From: Eduard Zingerman <eddyz87@gmail.com> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> --- tools/lib/bpf/bpf_core_read.h | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+)
Comments
On Tue, 2023-11-28 at 10:54 -0700, Daniel Xu wrote: > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield > writing wrapper to make the verifier happy. > > Two alternatives to this approach are: > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disable > CO-RE on specific structs. > 2. Use broader byte-sized writes to write to bitfields. > > (1) is a bit a bit hard to use. It requires specific and > not-very-obvious annotations to bpftool generated vmlinux.h. It's also > not generally available in released LLVM versions yet. > > (2) makes the code quite hard to read and write. And especially if > BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to > to have an inverse helper for writing. > > [0]: https://reviews.llvm.org/D133361 > From: Eduard Zingerman <eddyz87@gmail.com> > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > --- Could you please also add a selftest (or several) using __retval() annotation for this macro?
On Tue, Nov 28, 2023 at 07:59:01PM +0200, Eduard Zingerman wrote: > On Tue, 2023-11-28 at 10:54 -0700, Daniel Xu wrote: > > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield > > writing wrapper to make the verifier happy. > > > > Two alternatives to this approach are: > > > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disable > > CO-RE on specific structs. > > 2. Use broader byte-sized writes to write to bitfields. > > > > (1) is a bit a bit hard to use. It requires specific and > > not-very-obvious annotations to bpftool generated vmlinux.h. It's also > > not generally available in released LLVM versions yet. > > > > (2) makes the code quite hard to read and write. And especially if > > BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to > > to have an inverse helper for writing. > > > > [0]: https://reviews.llvm.org/D133361 > > From: Eduard Zingerman <eddyz87@gmail.com> > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > > --- > > Could you please also add a selftest (or several) using __retval() > annotation for this macro? Sure, I'll take a look. Thanks, Daniel
On Tue, Nov 28, 2023 at 07:59:01PM +0200, Eduard Zingerman wrote: > On Tue, 2023-11-28 at 10:54 -0700, Daniel Xu wrote: > > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield > > writing wrapper to make the verifier happy. > > > > Two alternatives to this approach are: > > > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disable > > CO-RE on specific structs. > > 2. Use broader byte-sized writes to write to bitfields. > > > > (1) is a bit a bit hard to use. It requires specific and > > not-very-obvious annotations to bpftool generated vmlinux.h. It's also > > not generally available in released LLVM versions yet. > > > > (2) makes the code quite hard to read and write. And especially if > > BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to > > to have an inverse helper for writing. > > > > [0]: https://reviews.llvm.org/D133361 > > From: Eduard Zingerman <eddyz87@gmail.com> > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > > --- > > Could you please also add a selftest (or several) using __retval() > annotation for this macro? Good call about adding tests -- I found a few bugs with the code from the other thread. But boy did they take a lot of brain cells to figure out. There was some 6th grade algebra involved too -- I'll do my best to explain it in the commit msg for v3. Here are the fixes in case you are curious: diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h index 7a764f65d299..8f02c558c0ff 100644 --- a/tools/lib/bpf/bpf_core_read.h +++ b/tools/lib/bpf/bpf_core_read.h @@ -120,7 +120,9 @@ enum bpf_enum_value_kind { unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ - unsigned int bit_size = (rshift - lshift); \ + unsigned int bit_size = (64 - rshift); \ + unsigned int hi_size = lshift; \ + unsigned int lo_size = (rshift - lshift); \ unsigned long long nval, val, hi, lo; \ \ asm volatile("" : "+r"(p)); \ @@ -131,13 +133,13 @@ enum bpf_enum_value_kind { case 4: val = *(unsigned int *)p; break; \ case 8: val = *(unsigned long long *)p; break; \ } \ - hi = val >> (bit_size + rshift); \ - hi <<= bit_size + rshift; \ - lo = val << (bit_size + lshift); \ - lo >>= bit_size + lshift; \ + hi = val >> (64 - hi_size); \ + hi <<= 64 - hi_size; \ + lo = val << (64 - lo_size); \ + lo >>= 64 - lo_size; \ nval = new_val; \ - nval <<= lshift; \ - nval >>= rshift; \ + nval <<= (64 - bit_size); \ + nval >>= (64 - bit_size - lo_size); \ val = hi | nval | lo; \ switch (byte_size) { \ case 1: *(unsigned char *)p = val; break; \ Thanks, Daniel
On Thu, 2023-11-30 at 18:33 -0700, Daniel Xu wrote: [...] > Good call about adding tests -- I found a few bugs with the code from > the other thread. But boy did they take a lot of brain cells to figure > out. > > There was some 6th grade algebra involved too -- I'll do my best to > explain it in the commit msg for v3. > > Here are the fixes in case you are curious: Ouch, I knew my code from 3am can't be trusted, sorry for that. Your math seem to make sense, thank you. [...]
On Thu, Nov 30, 2023 at 5:33 PM Daniel Xu <dxu@dxuuu.xyz> wrote: > > On Tue, Nov 28, 2023 at 07:59:01PM +0200, Eduard Zingerman wrote: > > On Tue, 2023-11-28 at 10:54 -0700, Daniel Xu wrote: > > > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield > > > writing wrapper to make the verifier happy. > > > > > > Two alternatives to this approach are: > > > > > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disable > > > CO-RE on specific structs. > > > 2. Use broader byte-sized writes to write to bitfields. > > > > > > (1) is a bit a bit hard to use. It requires specific and > > > not-very-obvious annotations to bpftool generated vmlinux.h. It's also > > > not generally available in released LLVM versions yet. > > > > > > (2) makes the code quite hard to read and write. And especially if > > > BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to > > > to have an inverse helper for writing. > > > > > > [0]: https://reviews.llvm.org/D133361 > > > From: Eduard Zingerman <eddyz87@gmail.com> > > > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > > > --- > > > > Could you please also add a selftest (or several) using __retval() > > annotation for this macro? > > Good call about adding tests -- I found a few bugs with the code from > the other thread. But boy did they take a lot of brain cells to figure > out. > > There was some 6th grade algebra involved too -- I'll do my best to > explain it in the commit msg for v3. > > > Here are the fixes in case you are curious: > > diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h > index 7a764f65d299..8f02c558c0ff 100644 > --- a/tools/lib/bpf/bpf_core_read.h > +++ b/tools/lib/bpf/bpf_core_read.h > @@ -120,7 +120,9 @@ enum bpf_enum_value_kind { > unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ > unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ > unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ > - unsigned int bit_size = (rshift - lshift); \ > + unsigned int bit_size = (64 - rshift); \ > + unsigned int hi_size = lshift; \ > + unsigned int lo_size = (rshift - lshift); \ nit: let's drop unnecessary () > unsigned long long nval, val, hi, lo; \ > \ > asm volatile("" : "+r"(p)); \ > @@ -131,13 +133,13 @@ enum bpf_enum_value_kind { > case 4: val = *(unsigned int *)p; break; \ > case 8: val = *(unsigned long long *)p; break; \ > } \ > - hi = val >> (bit_size + rshift); \ > - hi <<= bit_size + rshift; \ > - lo = val << (bit_size + lshift); \ > - lo >>= bit_size + lshift; \ > + hi = val >> (64 - hi_size); \ > + hi <<= 64 - hi_size; \ > + lo = val << (64 - lo_size); \ > + lo >>= 64 - lo_size; \ > nval = new_val; \ > - nval <<= lshift; \ > - nval >>= rshift; \ > + nval <<= (64 - bit_size); \ > + nval >>= (64 - bit_size - lo_size); \ > val = hi | nval | lo; \ this looks.. unusual. I'd imagine we calculate a mask, mask out bits we are replacing, and then OR with new values, roughly (assuming all the right left/right shift values and stuff) /* clear bits */ val &= ~(bitfield_mask << shift); /* set bits */ val |= (nval & bitfield_mask) << shift; ? > switch (byte_size) { \ > case 1: *(unsigned char *)p = val; break; \ > > > Thanks, > Daniel
On Fri, Dec 1, 2023 at 11:11 AM Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote: > > On Thu, Nov 30, 2023 at 5:33 PM Daniel Xu <dxu@dxuuu.xyz> wrote: > > > > On Tue, Nov 28, 2023 at 07:59:01PM +0200, Eduard Zingerman wrote: > > > On Tue, 2023-11-28 at 10:54 -0700, Daniel Xu wrote: > > > > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield > > > > writing wrapper to make the verifier happy. > > > > > > > > Two alternatives to this approach are: > > > > > > > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disable > > > > CO-RE on specific structs. > > > > 2. Use broader byte-sized writes to write to bitfields. > > > > > > > > (1) is a bit a bit hard to use. It requires specific and > > > > not-very-obvious annotations to bpftool generated vmlinux.h. It's also > > > > not generally available in released LLVM versions yet. > > > > > > > > (2) makes the code quite hard to read and write. And especially if > > > > BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to > > > > to have an inverse helper for writing. > > > > > > > > [0]: https://reviews.llvm.org/D133361 > > > > From: Eduard Zingerman <eddyz87@gmail.com> > > > > > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > > > > --- > > > > > > Could you please also add a selftest (or several) using __retval() > > > annotation for this macro? > > > > Good call about adding tests -- I found a few bugs with the code from > > the other thread. But boy did they take a lot of brain cells to figure > > out. > > > > There was some 6th grade algebra involved too -- I'll do my best to > > explain it in the commit msg for v3. > > > > > > Here are the fixes in case you are curious: > > > > diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h > > index 7a764f65d299..8f02c558c0ff 100644 > > --- a/tools/lib/bpf/bpf_core_read.h > > +++ b/tools/lib/bpf/bpf_core_read.h > > @@ -120,7 +120,9 @@ enum bpf_enum_value_kind { > > unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ > > unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ > > unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ > > - unsigned int bit_size = (rshift - lshift); \ > > + unsigned int bit_size = (64 - rshift); \ > > + unsigned int hi_size = lshift; \ > > + unsigned int lo_size = (rshift - lshift); \ > > nit: let's drop unnecessary () > > > unsigned long long nval, val, hi, lo; \ > > \ > > asm volatile("" : "+r"(p)); \ > > @@ -131,13 +133,13 @@ enum bpf_enum_value_kind { > > case 4: val = *(unsigned int *)p; break; \ > > case 8: val = *(unsigned long long *)p; break; \ > > } \ > > - hi = val >> (bit_size + rshift); \ > > - hi <<= bit_size + rshift; \ > > - lo = val << (bit_size + lshift); \ > > - lo >>= bit_size + lshift; \ > > + hi = val >> (64 - hi_size); \ > > + hi <<= 64 - hi_size; \ > > + lo = val << (64 - lo_size); \ > > + lo >>= 64 - lo_size; \ > > nval = new_val; \ > > - nval <<= lshift; \ > > - nval >>= rshift; \ > > + nval <<= (64 - bit_size); \ > > + nval >>= (64 - bit_size - lo_size); \ > > val = hi | nval | lo; \ > > this looks.. unusual. I'd imagine we calculate a mask, mask out bits > we are replacing, and then OR with new values, roughly (assuming all > the right left/right shift values and stuff) > > /* clear bits */ > val &= ~(bitfield_mask << shift); we can also calculate shifted mask with just bitfield_mask = (-1ULL) << some_left_shift >> some_right_shift; val &= ~bitfield_mask; > /* set bits */ > val |= (nval & bitfield_mask) << shift; > > ? > > > switch (byte_size) { \ > > case 1: *(unsigned char *)p = val; break; \ > > > > > > Thanks, > > Daniel
On Fri, Dec 01, 2023 at 11:13:13AM -0800, Andrii Nakryiko wrote: > On Fri, Dec 1, 2023 at 11:11 AM Andrii Nakryiko > <andrii.nakryiko@gmail.com> wrote: > > > > On Thu, Nov 30, 2023 at 5:33 PM Daniel Xu <dxu@dxuuu.xyz> wrote: > > > > > > On Tue, Nov 28, 2023 at 07:59:01PM +0200, Eduard Zingerman wrote: > > > > On Tue, 2023-11-28 at 10:54 -0700, Daniel Xu wrote: > > > > > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfield > > > > > writing wrapper to make the verifier happy. > > > > > > > > > > Two alternatives to this approach are: > > > > > > > > > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disable > > > > > CO-RE on specific structs. > > > > > 2. Use broader byte-sized writes to write to bitfields. > > > > > > > > > > (1) is a bit a bit hard to use. It requires specific and > > > > > not-very-obvious annotations to bpftool generated vmlinux.h. It's also > > > > > not generally available in released LLVM versions yet. > > > > > > > > > > (2) makes the code quite hard to read and write. And especially if > > > > > BPF_CORE_READ_BITFIELD() is already being used, it makes more sense to > > > > > to have an inverse helper for writing. > > > > > > > > > > [0]: https://reviews.llvm.org/D133361 > > > > > From: Eduard Zingerman <eddyz87@gmail.com> > > > > > > > > > > Signed-off-by: Daniel Xu <dxu@dxuuu.xyz> > > > > > --- > > > > > > > > Could you please also add a selftest (or several) using __retval() > > > > annotation for this macro? > > > > > > Good call about adding tests -- I found a few bugs with the code from > > > the other thread. But boy did they take a lot of brain cells to figure > > > out. > > > > > > There was some 6th grade algebra involved too -- I'll do my best to > > > explain it in the commit msg for v3. > > > > > > > > > Here are the fixes in case you are curious: > > > > > > diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h > > > index 7a764f65d299..8f02c558c0ff 100644 > > > --- a/tools/lib/bpf/bpf_core_read.h > > > +++ b/tools/lib/bpf/bpf_core_read.h > > > @@ -120,7 +120,9 @@ enum bpf_enum_value_kind { > > > unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ > > > unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ > > > unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ > > > - unsigned int bit_size = (rshift - lshift); \ > > > + unsigned int bit_size = (64 - rshift); \ > > > + unsigned int hi_size = lshift; \ > > > + unsigned int lo_size = (rshift - lshift); \ > > > > nit: let's drop unnecessary () > > > > > unsigned long long nval, val, hi, lo; \ > > > \ > > > asm volatile("" : "+r"(p)); \ > > > @@ -131,13 +133,13 @@ enum bpf_enum_value_kind { > > > case 4: val = *(unsigned int *)p; break; \ > > > case 8: val = *(unsigned long long *)p; break; \ > > > } \ > > > - hi = val >> (bit_size + rshift); \ > > > - hi <<= bit_size + rshift; \ > > > - lo = val << (bit_size + lshift); \ > > > - lo >>= bit_size + lshift; \ > > > + hi = val >> (64 - hi_size); \ > > > + hi <<= 64 - hi_size; \ > > > + lo = val << (64 - lo_size); \ > > > + lo >>= 64 - lo_size; \ > > > nval = new_val; \ > > > - nval <<= lshift; \ > > > - nval >>= rshift; \ > > > + nval <<= (64 - bit_size); \ > > > + nval >>= (64 - bit_size - lo_size); \ > > > val = hi | nval | lo; \ > > > > this looks.. unusual. I'd imagine we calculate a mask, mask out bits > > we are replacing, and then OR with new values, roughly (assuming all > > the right left/right shift values and stuff) > > > > /* clear bits */ > > val &= ~(bitfield_mask << shift); > > we can also calculate shifted mask with just > > bitfield_mask = (-1ULL) << some_left_shift >> some_right_shift; > val &= ~bitfield_mask; Yeah I was chatting w/ JonathanL about this and I've got basically that code ready to send for v3. Thanks, Daniel
diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h index 1ac57bb7ac55..7a764f65d299 100644 --- a/tools/lib/bpf/bpf_core_read.h +++ b/tools/lib/bpf/bpf_core_read.h @@ -111,6 +111,42 @@ enum bpf_enum_value_kind { val; \ }) +/* + * Write to a bitfield, identified by s->field. + * This is the inverse of BPF_CORE_WRITE_BITFIELD(). + */ +#define BPF_CORE_WRITE_BITFIELD(s, field, new_val) ({ \ + void *p = (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned int byte_size = __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned int lshift = __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned int rshift = __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned int bit_size = (rshift - lshift); \ + unsigned long long nval, val, hi, lo; \ + \ + asm volatile("" : "+r"(p)); \ + \ + switch (byte_size) { \ + case 1: val = *(unsigned char *)p; break; \ + case 2: val = *(unsigned short *)p; break; \ + case 4: val = *(unsigned int *)p; break; \ + case 8: val = *(unsigned long long *)p; break; \ + } \ + hi = val >> (bit_size + rshift); \ + hi <<= bit_size + rshift; \ + lo = val << (bit_size + lshift); \ + lo >>= bit_size + lshift; \ + nval = new_val; \ + nval <<= lshift; \ + nval >>= rshift; \ + val = hi | nval | lo; \ + switch (byte_size) { \ + case 1: *(unsigned char *)p = val; break; \ + case 2: *(unsigned short *)p = val; break; \ + case 4: *(unsigned int *)p = val; break; \ + case 8: *(unsigned long long *)p = val; break; \ + } \ +}) + #define ___bpf_field_ref1(field) (field) #define ___bpf_field_ref2(type, field) (((typeof(type) *)0)->field) #define ___bpf_field_ref(args...) \