Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1409226rdb; Fri, 1 Dec 2023 16:14:01 -0800 (PST) X-Google-Smtp-Source: AGHT+IG6vAR10I7VqIlmZwFVggOCfisa79wiMHUe5484Yw83hWGx2BlTCS77wkmYkCN4RhIDwr9C X-Received: by 2002:a05:6870:6122:b0:1fb:642:9ccd with SMTP id s34-20020a056870612200b001fb06429ccdmr491215oae.31.1701476041569; Fri, 01 Dec 2023 16:14:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701476041; cv=none; d=google.com; s=arc-20160816; b=Qir9REWbiA4Ht2VuBixdbPNeDrFmxyEsgRdzsNxdPSm3os+WSYzv3iVrHFJndlfvyW /uZvW+o6Bq0aU5Yt1CwxiUIbNrhSu90N/t8gNvkUgfzJWBMbPqYXoqFytIA1UGTYLS7U nWK6afP/zy4XyMpVVSGzLAZWxu4hvuNynzCbs6L3y3b6Fqy9zxSI7bGkWl/sa+2+7tn/ Grxlpp11y3H45v4ECyO8ULGO/GAYVxRUPjQn9o9P//Y6ZQR2gKGbclkm5HGuPNZrAeQ0 4DCvz0q6JssMHSbLqrXmx9/KsTOB1Xy5V69djU3uij2Boa36muq+0RxDF1vbAVpeMmys D2Cw== 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=4VH9n7Ku5hozb2YWSefaThME8FC9SFCVYUNSDmL4YdM=; fh=uGDjAyLyTAXhyN1F56DOADsyNARhmfXLM9kgKyqO1OE=; b=04K2HpYt0trgcp2yJkJrZOBzsMWY/LCuhXVuIsW9599UXNvhiqQUCrmC8IhUs8c3Ha 0dxOfifQ5y8grFwH2ny5gM4Q8fyZqf6Awrjex24Nf2IhE7lHZfdUuha41R9YAvt00XxI SEqlUK7a2TKFw7VL56CmFr+C0L0FszKM68sCYweyp8FYoDAjh37Um+dsW7KFwE7ijCqz tGG/45wEs8nTHIbA25Zq2/fKxecmnqOjszg4wxFrkMkYbEmeFx7pb7GRs7KrP+BYO5Ih U/VvdPxLKcgkyjnJGn77xprWAnEMd80kh3ZEAmoc9ucIttzKv7u+y2zZRC8e3kkg4ivP 7rBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=QvGPVhus; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=cKHKI6jO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id h66-20020a636c45000000b005bdc61e1793si4026373pgc.358.2023.12.01.16.14.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:14:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=QvGPVhus; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=cKHKI6jO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 fry.vger.email (Postfix) with ESMTP id 6C9BC80859BC; Fri, 1 Dec 2023 16:13:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441901AbjLBANm (ORCPT + 99 others); Fri, 1 Dec 2023 19:13:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjLBANl (ORCPT ); Fri, 1 Dec 2023 19:13:41 -0500 Received: from new2-smtp.messagingengine.com (new2-smtp.messagingengine.com [66.111.4.224]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9CC36DD; Fri, 1 Dec 2023 16:13:46 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailnew.nyi.internal (Postfix) with ESMTP id E055B580E80; Fri, 1 Dec 2023 19:13:45 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Fri, 01 Dec 2023 19:13:45 -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= 1701476025; x=1701483225; bh=4VH9n7Ku5hozb2YWSefaThME8FC9SFCVYUN SDmL4YdM=; b=QvGPVhusvl9uXE5SCEbCr+HKEXCi9wla/7/ufCMQBCxoSKrK4BF ZCmC+55SqeuqJxzpSTRdHZbkH/QLj5ldcbNYaed5mc4Ez0+/tJmkoyMP4DZh83D7 PJbzMnLoc6SJ89R3iXBiAQK8BMZB8kKaKTSG3q/rcbiAgC1h525URljrKDgRjxZK CP8uCjNBo+w/oUK6Qk0jVoeodNRn22gfvR0ir5xI7+aMYPZIAWAtbtxfSVpGu7SB GQD+3JR0CbkIo6fiqHAwNc3pMpq9pmYrvZ5vthiy4FuSuFNyuy300LjjokylMqKc KZpmtqSJwP+3tFMWU8zE1GXc8xfR6xo6YmA== 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= 1701476025; x=1701483225; bh=4VH9n7Ku5hozb2YWSefaThME8FC9SFCVYUN SDmL4YdM=; b=cKHKI6jOPonL6KUhmHxbE03zRkA9ocQmetXDH9o3wAJx+sYrwsC f407l25U8ohJ/u7e4JfJkPECw3ghM8XssDWQJFKP+NrdvWbGytN9o+dtIJPDwonz 62cSu9VLUojap9mV0BcBKiJI7ZXceqa5psNR8t5fwRKHXv1MvscUQU9e2eyI2iTU tkVW3sdUgpAOvaKwiexBnQn3J+vGy7WxInX82Q1Ma1AsBEa0o2GH/L3B7dXpy949 aJF5qCzXQydCt3BZPv4naUft40cWBtuB9kniRE45iGiUAmsrI1dgJbJKF59PVMHa wDxa+Ni7o7+YHWnm1sk8scZPSbOQEa3w1Yg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudejtddgvddtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlvdefmdenucfjughrpeffhffvvefukfhfgggtugfgjgestheksfdt tddtjeenucfhrhhomhepffgrnhhivghlucgiuhcuoegugihusegugihuuhhurdighiiiqe enucggtffrrghtthgvrhhnpefhudffleefvdefvdevudehfeevjeegkefftdetueettedu iefhkedvgfdtgffhveenucffohhmrghinheplhhlvhhmrdhorhhgnecuvehluhhsthgvrh fuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepugiguhesugiguhhuuhdrgiih ii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 19:13:43 -0500 (EST) Date: Fri, 1 Dec 2023 17:13:42 -0700 From: Daniel Xu To: Andrii Nakryiko Cc: ndesaulniers@google.com, daniel@iogearbox.net, nathan@kernel.org, ast@kernel.org, andrii@kernel.org, steffen.klassert@secunet.com, antony.antony@secunet.com, alexei.starovoitov@gmail.com, yonghong.song@linux.dev, eddyz87@gmail.com, 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, Jonathan Lemon Subject: Re: [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro Message-ID: References: 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 fry.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 (fry.vger.email [0.0.0.0]); Fri, 01 Dec 2023 16:13:58 -0800 (PST) On Fri, Dec 01, 2023 at 03:49:30PM -0800, Andrii Nakryiko wrote: > On Fri, Dec 1, 2023 at 12:24 PM Daniel Xu wrote: > > > > === Motivation === > > > > 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 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. > > > > === Implementation details === > > > > Since the logic is a bit non-obvious, I thought it would be helpful > > to explain exactly what's going on. > > > > To start, it helps by explaining what LSHIFT_U64 (lshift) and RSHIFT_U64 > > (rshift) is designed to mean. Consider the core of the > > BPF_CORE_READ_BITFIELD() algorithm: > > > > val <<= __CORE_RELO(s, field, LSHIFT_U64); > > val = val >> __CORE_RELO(s, field, RSHIFT_U64); > > nit: indentation is off? Oops, it's cuz I only deleted the SIGNED check. Will fix. > > > > > Basically what happens is we lshift to clear the non-relevant (blank) > > higher order bits. Then we rshift to bring the relevant bits (bitfield) > > down to LSB position (while also clearing blank lower order bits). To > > illustrate: > > > > Start: ........XXX...... > > Lshift: XXX......00000000 > > Rshift: 00000000000000XXX > > > > where `.` means blank bit, `0` means 0 bit, and `X` means bitfield bit. > > > > After the two operations, the bitfield is ready to be interpreted as a > > regular integer. > > > > Next, we want to build an alternative (but more helpful) mental model > > on lshift and rshift. That is, to consider: > > > > * rshift as the total number of blank bits in the u64 > > * lshift as number of blank bits left of the bitfield in the u64 > > > > Take a moment to consider why that is true by consulting the above > > diagram. > > > > With this insight, we can how define the following relationship: > > > > bitfield > > _ > > | | > > 0.....00XXX0...00 > > | | | | > > |______| | | > > lshift | | > > |____| > > (rshift - lshift) > > > > That is, we know the number of higher order blank bits is just lshift. > > And the number of lower order blank bits is (rshift - lshift). > > > > Nice diagrams and description, thanks! Thanks! > > > Finally, we can examine the core of the write side algorithm: > > > > mask = (~0ULL << rshift) >> lshift; // 1 > > nval = new_val; // 2 > > nval = (nval << rpad) & mask; // 3 > > val = (val & ~mask) | nval; // 4 > > > > (1): Compute a mask where the set bits are the bitfield bits. The first > > left shift zeros out exactly the number of blank bits, leaving a > > bitfield sized set of 1s. The subsequent right shift inserts the > > correct amount of higher order blank bits. > > (2): Place the new value into a word sized container, nval. > > (3): Place nval at the correct bit position and mask out blank bits. > > (4): Mix the bitfield in with original surrounding blank bits. > > > > [0]: https://reviews.llvm.org/D133361 > > Co-authored-by: Eduard Zingerman > > Signed-off-by: Eduard Zingerman > > Co-authored-by: Jonathan Lemon > > Signed-off-by: Jonathan Lemon > > Signed-off-by: Daniel Xu > > --- > > tools/lib/bpf/bpf_core_read.h | 34 ++++++++++++++++++++++++++++++++++ > > 1 file changed, 34 insertions(+) > > > > diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h > > index 1ac57bb7ac55..a7ffb80e3539 100644 > > --- a/tools/lib/bpf/bpf_core_read.h > > +++ b/tools/lib/bpf/bpf_core_read.h > > @@ -111,6 +111,40 @@ 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 rpad = rshift - lshift; \ > > + unsigned long long nval, mask, val; \ > > + \ > > + 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; \ > > + } \ > > + \ > > + mask = (~0ULL << rshift) >> lshift; \ > > + nval = new_val; \ > > + nval = (nval << rpad) & mask; \ > > + val = (val & ~mask) | nval; \ > > I'd simplify it to not need nval at all > > val = (val & ~mask) | ((new_val << rpad) & mask); > > I actually find it easier to follow and make sure we are not doing > anything unexpected. First part before |, we take old value and clear > bits we are about to set, second part after |, we take bitfield value, > shift it in position, and just in case mask it out if it's too big to > fit. Combine, done. > > Other than that, it looks good. I mostly left it there for the cast. Cuz injecting the `unsigned long long` cast made the line really long. How about this instead? diff --git a/tools/lib/bpf/bpf_core_read.h b/tools/lib/bpf/bpf_core_read.h index a7ffb80e3539..7325a12692a3 100644 --- a/tools/lib/bpf/bpf_core_read.h +++ b/tools/lib/bpf/bpf_core_read.h @@ -120,8 +120,8 @@ 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 long long mask, val, nval = new_val; \ unsigned int rpad = rshift - lshift; \ - unsigned long long nval, mask, val; \ \ asm volatile("" : "+r"(p)); \ \ @@ -133,9 +133,7 @@ enum bpf_enum_value_kind { } \ \ mask = (~0ULL << rshift) >> lshift; \ - nval = new_val; \ - nval = (nval << rpad) & mask; \ - val = (val & ~mask) | nval; \ + val = (val & ~mask) | ((nval << rpad) & mask); \ \ switch (byte_size) { \ case 1: *(unsigned char *)p = val; break; \ Thanks, Daniel