Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp728420rdb; Thu, 30 Nov 2023 17:34:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IEIevZ2JLlFKjlrY5Cd+HMLC0Kd7vxZB9UwYFVyenrW518wRyw3LRQQhIHR1YixFQabGH82 X-Received: by 2002:a05:6a21:a58d:b0:18b:d99a:99bd with SMTP id gd13-20020a056a21a58d00b0018bd99a99bdmr28099621pzc.32.1701394440398; Thu, 30 Nov 2023 17:34:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701394440; cv=none; d=google.com; s=arc-20160816; b=AJKKGm9RLk3qiNm/ujPSahqFKEfPOGA1IRvcuqM+CmeWJ7alKHeajdeG5TFXswWKJO 2Lmx/vW+dL0Kt9gNjqh/HXRW6QuFFjz1R2Eb1hMkaHxK2EB21ALyYJ1ylZbyo53LRfne g9FYyoYmQgiK7KWuqgWurimxpcvCJwu/ii0E0yFEN9KV+Dzkbe8V0o0mW7Bdtc2w4Lra bCvHuL9H6pjNPkbH+v5Qn7wfzux4PTPOevYWpWeHTdkmp3M0Sv/o3KFBV3ZsIWwqT8Nf LjNaS5P0YVmxX71oFif9id5Xx0MXKQQ4o1IxbSwnbk5n+ND+h/+XppNovEKi4X+UlfB+ NLzg== 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:cc:to:from:date:feedback-id :dkim-signature:dkim-signature; bh=jfqYqY018jdffj6zz+dnM/tM+okukGD8jcdyViM8tLU=; fh=/egugHjxSwer1PwZhrTi3aNTvuaXdmOpRXiAqLbb8d8=; b=YFUMPRp0XTXqj4z4lPcBYyogS/E27H5hlSANzO/B5ImFuzCXvmpJym7gTW1J1mw6Th eJXGnpaRnoiSxRgS4qZcv9fkM7lhPErb0iZRksc3bL7tPbI+Pr/5CwPZ7YNG+OXu5Y4/ jSi13M4HwAgLBDs1qfQ9C9IAbMExLjyzdioHzdhhmIvbKm5jH+VwVkIBBkxsHttMdMJ7 FZDxsPFEgfrToiggpn9qaVrNqGVCZwarxXzkJPa7LQr9vZ9eIBYm1rtXt57Nx9vy9ggp Wz7fVw6JZmcEyG//x3RDMmrhL8b35XK/2JiV3NKqQbKJt9Vf5MN+a3D/7ZSHMWk+u45h iIYg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=iFG2UTmm; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=E4JAUDbt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id i5-20020a17090a974500b00285807d0f78si4744886pjw.86.2023.11.30.17.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 17:34:00 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@dxuuu.xyz header.s=fm3 header.b=iFG2UTmm; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=E4JAUDbt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id 1F3448083B45; Thu, 30 Nov 2023 17:33:56 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229577AbjLABdj (ORCPT + 99 others); Thu, 30 Nov 2023 20:33:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229448AbjLABdi (ORCPT ); Thu, 30 Nov 2023 20:33:38 -0500 Received: from new3-smtp.messagingengine.com (new3-smtp.messagingengine.com [66.111.4.229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5BDDD50; Thu, 30 Nov 2023 17:33:44 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailnew.nyi.internal (Postfix) with ESMTP id 9CEAD580A5A; Thu, 30 Nov 2023 20:33:41 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Thu, 30 Nov 2023 20:33:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :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=1701394421; x=1701401621; bh=jf qYqY018jdffj6zz+dnM/tM+okukGD8jcdyViM8tLU=; b=iFG2UTmmYzmIB4IoIc nRRmjJw9ay21fZLZfnHBcmxVZCkud4yQJ+V9Auwh19vxjk5IoliLkrjmAanvRp9e ILdJ8v4jD4l+IXTgw0YXlWdwIRAeYzXhMWrfnjpjiB2Q6hEOkcnz+0HNk5N55d/k efHhKzvnCQvHUaSLxPUxMp4hT/DY6XxGq9JM8N4v8aCA6coqclgEsg3+LqsIH3sp e3mtVu0eBzGZTErTmJguLh6BDlls9iALhKiLf1VW0lJCfOads73lg+IGVAJseXML AtANjKgh5WvRiZ4WlfzH+l09eNHsmjvikUOOw8XBvOJL2LHVSwgp8nDSZsWNL3I7 ZTYA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=1701394421; x=1701401621; bh=jfqYqY018jdff j6zz+dnM/tM+okukGD8jcdyViM8tLU=; b=E4JAUDbt3CWm62uu3AwTKuhYnxZSm s8urrS3AHDstYIGUMr9sE0iQiTKfsGSSgy50QWr1EfyV2qjlVid/jObea8rfMlb5 Mh1hnFHOUzkSzcGiUKzzSI7IQow1lbAB8jkxRdNyS3lga63GM3YraUghVDqwxg9c 5ycJzB8zd62g7ScwwDWsnYtd1vBr7Yx/HwxHuYEQbGwyOf+dBqGxnhYwAXoyp4Os ej6Khe8SWeNp1+iX0JZ9Wi0FF1UWDHNKQn8I8gEEwmyaoAYRlCLDAa8aUvqyqMDz e1odz7Ai4fpIS2SEHEpqI6h1iTNx70pB4qvNHwy3ghWl3xZoDHbFmqHng== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrudeikedgfeeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgfrhhlucfvnfffucdlvdefmdenucfjughrpeffhf fvvefukfhfgggtuggjsehttdfstddttddvnecuhfhrohhmpeffrghnihgvlhcuighuuceo ugiguhesugiguhhuuhdrgiihiieqnecuggftrfgrthhtvghrnhepfedtffeuudehheduhf eggfefjeelhfetgeeiteevieeiheefvdehudeifedvfedunecuffhomhgrihhnpehllhhv mhdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 30 Nov 2023 20:33:39 -0500 (EST) Date: Thu, 30 Nov 2023 18:33:37 -0700 From: Daniel Xu To: Eduard Zingerman Cc: 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=us-ascii Content-Disposition: inline In-Reply-To: <20c593b6f31720a3d24d75e5e5cc3245b67249d1.camel@gmail.com> 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 howler.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 (howler.vger.email [0.0.0.0]); Thu, 30 Nov 2023 17:33:56 -0800 (PST) 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); \ 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