Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1320635rdb; Fri, 1 Dec 2023 12:52:03 -0800 (PST) X-Google-Smtp-Source: AGHT+IGKTzxa6OCIiF48r+vqoiAW4dd43mfYoYX/NQsOBx7Y1xjjFMn4wVo7NKe3Wnh/xQ/db71q X-Received: by 2002:a05:6a00:22cb:b0:6ce:2731:5f76 with SMTP id f11-20020a056a0022cb00b006ce27315f76mr129336pfj.53.1701463922985; Fri, 01 Dec 2023 12:52:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701463922; cv=none; d=google.com; s=arc-20160816; b=nzoGtjC2XZetuuvKT5O/XlMwSuqcCndjyv+fbLHRNwFe63RA3k3OoZFh2K9C56IBbh q2mn+oJfSyIDpP1JjrN+v1f6nK+avRMPPz70rSa4cqei0JVXgsQemvk04t9oQFyNQl8H SoxagEAe1lvCN1VETneg6w0+kZsAEz//ge4iHanKQSvYA5UpPiePv+/pRYXr/g36XHTN 1K5Say8W+co5u38u2zR4H9IqIh1HNp3AkrDVttCQ3S1/FlGCPEEzK5OHaaQWTljyQRji YxIVvkyHv3pp9MFvEB9ai5dypgVlRHsjqMkYmdmrbnZeixGT8xQjPwI5d2dHgafGoict CC9Q== 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-disposition:mime-version :references:message-id:subject:to:from:date:feedback-id :dkim-signature:dkim-signature; bh=nK6LDD0U/Eoft2OMP2N2jIS27TuNzocTptkvpWvSp38=; fh=UQ9ZFZf97IoSycnr2qS8kjC4GyMA4U8nYkNG5Ofh2ac=; b=PaBJG846i0LJcExKT/b1YSsmYzCBKQdBZZ51LF2FRrhkNwpwC1gXjhldu+Y9d8Ozo1 03OYeOGbT272UgyUnjkvPty5bRjweBE8wPK7jl+/VH1BUBwoCHUseCgPVtVBDV7Bv5/y 6FL0dwDvL8Fb8knV08xwJl4yS5eSdhw3Suq0YWNCIVx2QsI2dqsLobycQFEdfr7zuCWg Cl1k1sNO2ELbyLgLKPKWciqA8dTyUSrFOE2peRp9aFfDbukspJX8mdPAXpTjhInO1uUI oAGSdPgLk3FfzI+Kp2vjp0D4BA0w5VJ7hYbqXmnx03c1nzAlURFdgVJ8TitYCmlKXxwc mQTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=nIg0wlqJ; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=KlTtA6eZ; 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 Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id o186-20020a62cdc3000000b006cd9f3d387asi3727536pfg.277.2023.12.01.12.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 12:52:02 -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=nIg0wlqJ; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=KlTtA6eZ; 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 3658F81C9A55; Fri, 1 Dec 2023 12:52:00 -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 S230453AbjLAUvo (ORCPT + 99 others); Fri, 1 Dec 2023 15:51:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229456AbjLAUvm (ORCPT ); Fri, 1 Dec 2023 15:51:42 -0500 Received: from new1-smtp.messagingengine.com (new1-smtp.messagingengine.com [66.111.4.221]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C91C710D; Fri, 1 Dec 2023 12:51:48 -0800 (PST) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.nyi.internal (Postfix) with ESMTP id 1616B580C55; Fri, 1 Dec 2023 15:51:48 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Fri, 01 Dec 2023 15:51:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :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=1701463908; x=1701471108; bh=nK 6LDD0U/Eoft2OMP2N2jIS27TuNzocTptkvpWvSp38=; b=nIg0wlqJmQgXCJuSDe 92opTTkESihZX/Vg+hr56JZfOTb/wJHRhYAT0gFgqhaMf0UFj+sQ9YOEBZzb4THf hVsLO+HLag8r8iXKOlqvQrJwj7Q7mjfpwCjuzfzdqkL839ubPiI83D1rSVLKkn3T X0f+ypmSuv4i17xXHAQ4hbaZcND+vagqN5lHW1g7ffj0+Pwrh61aR9W8hVlFwfbf +nisRjKKfWSvmcfZOZ9SFR90/1vWF2gbFi6gLSRECJ+WcZcvZF//G2eRazd/4MYl n+bxHhlZoqpczSaZac477GzLBCoCd/TIaAYsXT4ECq0BYpUiKj0zPy3fWZzmTKWQ qleA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1701463908; x=1701471108; bh=nK6LDD0U/Eoft 2OMP2N2jIS27TuNzocTptkvpWvSp38=; b=KlTtA6eZzg0M3pIQr2eDndN+IiA/W QJ+ZVpHZzSIG27e/3SRLqTZBx4B/hO2kdBseqEgy9ERGf+aB+pE201vJ19clUMSh 3cMMl9+qjM4+jfzpX8zpUnv8eqVj0oYQbkgUPDtn72poDdvrJqTcDIEDCEnb7oeq ENf1qiMzvajJGsVoBKudmk37r3sRu/jKOCYfXnvt26mUEzksbi5lprCz7thOo4ox 23I7aElDYSVI3XgFMNAxqs9xsjzmPCNtgAFPWfchnUPy6HdRfGpx69GozTImpjel h3AA2zJetUZaAH2Y6SKGxrFQtVzEpAZkky3nYVH7J4jI3sTt1r4XCjxDA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeiledgudegvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculddvfedmnecujfgurhepfffhvffukfhfgggtuggjsehttdfstddt tddvnecuhfhrohhmpeffrghnihgvlhcuighuuceougiguhesugiguhhuuhdrgiihiieqne cuggftrfgrthhtvghrnhepkeehjeekudeltdeljeehhfefjeevjefgudfhteehlefftdfh hfduieeiieeifeelnecuffhomhgrihhnpehllhhvmhdrohhrghenucevlhhushhtvghruf hiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 1 Dec 2023 15:51:46 -0500 (EST) Date: Fri, 1 Dec 2023 13:51:44 -0700 From: Daniel Xu To: 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: [devel-ipsec] [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro Message-ID: <2schji4oladptrev3tswmwkbhspz6mdy5u2v7tvll4du7iylri@2u2zmfdzn6fm> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Fri, 01 Dec 2023 12:52:00 -0800 (PST) On Fri, Dec 01, 2023 at 01:23:14PM -0700, Daniel Xu via Devel 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); > > 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). > > 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 Just pointing out I inserted Eduard's tags here. Eduard - I hope that's OK. Not sure what the usual procedure for this is. > 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(+) > [..]