Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1298372rdb; Fri, 1 Dec 2023 12:07:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IEjclyvhlKRFnB3fhBTpaG5Pbd4lZXWn8xyrhk6AkHHI0tEZj5XbsE1s//JwGxVZoNzUL2F X-Received: by 2002:a05:6358:2484:b0:16d:edaa:921c with SMTP id m4-20020a056358248400b0016dedaa921cmr175240rwc.12.1701461230241; Fri, 01 Dec 2023 12:07:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701461230; cv=none; d=google.com; s=arc-20160816; b=ccCmzDkMubyeOuzC5zubaIRt/3xuDtgFiP4zoam887iWivcWbxun1DjSrw9kMPdg0H 5D+5QD6ZhwUH6/DVSeEFDQzPhbTLRdqBBumemwuwAkM+VC93c+hvKRPdj+kvU+4qTNBF 5tXAYwcKlmeZIDDbqKfRBcMI33Uw+hTzAVv9GRu4zvUZJzMPVbWVt5nf3TSKrko+Wn/2 cvVknpttrOc/8k7DfOuCt4Yzxhyheodgk1oiwhw8HfKHq5MG/MXkUbJBHbrrrDRgW8O2 8LSi7nmMHnyXVxFBJ9sq3JROuFtl16fRZdO206jvmjhu32pqAQk5WOLhElXDSkPqKfc7 yhag== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature:dkim-signature; bh=/u3yLH29k8T5r7NJxXWGv9BlZGGVl+1F2lszusvsbJ0=; fh=mGrK0YbYiGRV3fFOq83YSu5o5IokW6WlwwUQkgRPmBo=; b=zBRDL2wr9jTuamhLC8E7Ow6wxB5UHgngdhcnip8wVXChQhuq8r1wiihmhzucwxTlS5 YmAi91PVDEinqIagYI2FSyJjeHz6X8qssVqi4E8brx7I/eoMe6xWQiA5CfoKKy/YZnjF Ze1DktW37TSkWTFUyzTxery1aDLRrlosNKAcsGjxZGmcx8PczEJMHQ6awhq+bQ4Hz06Q 9/Zh3JkXpT3l5kV/NjHN8P873FMPak20wFPDZUnfOIscAfFbXBo5CsOXEmzpaCJJmczH AY/BhxQvTreerT1HGzNaWnDpZGdWcY/oAl+At3TlFqztajPzRrs7mWYpt679e2ZhEyh+ REUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=qGpYgkjN; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=Z3I5vxLf; 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 Return-Path: Received: from pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id c7-20020a656747000000b005c603f06562si4157187pgu.333.2023.12.01.12.07.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:07:10 -0800 (PST) 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=@dxuuu.xyz header.s=fm3 header.b=qGpYgkjN; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=Z3I5vxLf; 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 Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 605878075588; Fri, 1 Dec 2023 12:06:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379582AbjLAUFu (ORCPT + 99 others); Fri, 1 Dec 2023 15:05:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379559AbjLAUFs (ORCPT ); Fri, 1 Dec 2023 15:05:48 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34F3EB2; Fri, 1 Dec 2023 12:05:54 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailnew.nyi.internal (Postfix) with ESMTP id A7F0C580773; Fri, 1 Dec 2023 15:05:51 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Fri, 01 Dec 2023 15:05:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type: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= 1701461151; x=1701468351; bh=/u3yLH29k8T5r7NJxXWGv9BlZGGVl+1F2ls zusvsbJ0=; b=qGpYgkjNlqWggIHiSu4D3RZgSJ3nBcLv5OqMeVZguaCQyi5QNEp 9cgeMwa9X8sJ3Ttg4uh0ZXAOpEuILMhkgqCRSofFtZAsT3NguXWGTJJPxiwHmjw4 UwnZ24pzEHzKJ24Rz869muRs25eB97k+q9lK8l7LOEHAs1VyQBu920DxKPzLnGiG ofq/PLmKQo4Q58loWslypGUJksj1Vyz3z3/Z4B1FyQgzeTftTkjLasamFTBTGxLP XOt+dMB6O6UEO7QYzAsRYmAepXsFt+GDBbaY+O8HrVB0KH+CFaewQqaJ8HbxJi5L yzjtAWAABEJoqsVOEX7rmVwt0FhFDTFoqsQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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= 1701461151; x=1701468351; bh=/u3yLH29k8T5r7NJxXWGv9BlZGGVl+1F2ls zusvsbJ0=; b=Z3I5vxLf2sF9upN8LhfnCJPBDEtQYg/Tr9l3XRyFdiPuxKxN2Y7 ghg4kqphZf5+YkfnbNHwLbsWZL8Iubi4WPmld0ucsGXWWCYpFkt8xgismuAf0RSc tC1WUGs0ffZe73UUQDj2n+keugJizzrBPpA3a21fpnZhpfJ9/+qwJs0UmRGIl9zh KZCvKN3rbIGzjq03qzMvNwiO8IB9huNIlVXD7qawLbEkj4tH+Pd/nC83sAphOwZs z1O6dtu5eG4fPByRtKeBt96owIMuIJnkRltvgjnYopqanbEjbohRcTt5ONjWzoRJ qEEkifcAU4d6brV1I7HkPdMv2Oznd5MWlAg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudeffecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculddvfedmnecujfgurhepfffhvfevuffkfhggtggugfgjsehtkefs tddttdejnecuhfhrohhmpeffrghnihgvlhcuighuuceougiguhesugiguhhuuhdrgiihii eqnecuggftrfgrthhtvghrnhephfduffelfedvfedvveduheefveejgeekffdtteeuteet udeihfekvdfgtdfghfevnecuffhomhgrihhnpehllhhvmhdrohhrghenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdig hiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:05:49 -0500 (EST) Date: Fri, 1 Dec 2023 13:05:47 -0700 From: Daniel Xu To: Andrii Nakryiko Cc: Eduard Zingerman , 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, 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: Re: [PATCH ipsec-next v2 3/6] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro Message-ID: References: <20c593b6f31720a3d24d75e5e5cc3245b67249d1.camel@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: 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 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]); Fri, 01 Dec 2023 12:06:10 -0800 (PST) On Fri, Dec 01, 2023 at 11:13:13AM -0800, Andrii Nakryiko wrote: > On Fri, Dec 1, 2023 at 11:11 AM Andrii Nakryiko > wrote: > > > > On Thu, Nov 30, 2023 at 5:33 PM Daniel Xu 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 > > > > > > > > > > Signed-off-by: Daniel Xu > > > > > --- > > > > > > > > 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