Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1268210rdb; Fri, 1 Dec 2023 11:14:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IGDEmCZYBQ30o4DhwzhCEF8C/heP8OLmn7ISJsV0e3fxmPKRZyRT5/Kvsc85caV4yJZpB15 X-Received: by 2002:a05:6a20:94cc:b0:18b:b02d:8b6 with SMTP id ht12-20020a056a2094cc00b0018bb02d08b6mr97293pzb.23.1701458036601; Fri, 01 Dec 2023 11:13:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701458036; cv=none; d=google.com; s=arc-20160816; b=fWZSLze7hmsbpdwCVX3vvFjjbQY57Eeu3PC/tlZN/0otrSYZragtAai3GR5k7sYCId h0ObJqaKknM9hYRcnuG5xLB/alKvB7oD+j09kH45fHBT8e8TSXcJIvbdKhwKonQsWSYs iiwqyWAVmjwdQqmEvrjx0J4RY0+Fi8dqvgm9hCfAxcJ/a2NBQCl/2loHsDKa5D1TImm1 9HKqi1/ABzOe8OylM8yAqXWPamEBcTw3ztxHrmNqSn1No2lmv8moZLYvN7ERikgL/OfO idWCFdrHvIK8OlKrKyavLT93UfujD9GxGmarGGqwY+SJ2tEk+Y2z2/j3wh3mu4JT5hC/ isqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=i2Lbiqp5ZRvN1hAivWbYt8v17Pu/BS70LAaQOJ42RIQ=; fh=fNrwRB0Z9fX+Q8B04IhS4mFwzyepgmKKkf3s70lwA6c=; b=IXAx/a8sOb+KazsvAPai2j0inx4H3R6xXaz7l1Pj7N+C0JA0wjWfHZ5fvpbqF5atYJ 4RvA4bA/RhjngVu5am2R5NUMKt5OjrNhRTyp+OTtbIAOM7iMTZVc93dOn1FlAOSLuZIk ggQ8AGiChf4EM5xMLQtOglHdHk/0WDcZ1H0u2lFaJNt3hnm1NdWPoTnXQUptnoriiXsG TAFMvu/U8HhQjq8Y1IQXY05IMUKNcw/0QLYgMpRw8dYOfu2zELSMEgZHNOr9Zvwk8q/T uhNRBO+5UQ72JOMFpzhIJVKphyEC5KvsxUMibwcQ7lNYMX3gr3IYudel5GEWHp45v4nR aBSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=S4OgT3GJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id k9-20020aa788c9000000b0069109ee0b59si586449pff.231.2023.12.01.11.13.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 11:13:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=S4OgT3GJ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id 5A1D8832CBE4; Fri, 1 Dec 2023 11:13:44 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379489AbjLATNV (ORCPT + 99 others); Fri, 1 Dec 2023 14:13:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379411AbjLATNV (ORCPT ); Fri, 1 Dec 2023 14:13:21 -0500 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C8958E; Fri, 1 Dec 2023 11:13:27 -0800 (PST) Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a195e0145acso226486966b.2; Fri, 01 Dec 2023 11:13:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701458005; x=1702062805; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=i2Lbiqp5ZRvN1hAivWbYt8v17Pu/BS70LAaQOJ42RIQ=; b=S4OgT3GJnn1F1kv+0iYK2iLNzjM17wuRavrToI8BhQactkOMdPhWgHrhIS+GJmnFi/ qQ2HkOj1l3ll/Jxb01jUa8uzuTo1+keEjqeVIcTf+yKhjNa5ITbCQPmzQ7ecNpg5S3KA vYsZsUV5BkIubLflJUgBkQfCiG/lHpkfGKtXTWjognbrK4U0yd5yye8jx8GhCrT2UabT MnztDRnsiyms1Ziv81d1iNTmmfkEwLlNGtFEBQ0u1dJvcBrYTlwa/2FTjQPP+6AO6QHv 8yy5aycpSNb6uGCETFeB05tybdu3cyPGaT56E63GduL+1CbXVaW6v+WGIs8k1ewSoCo/ 67tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701458005; x=1702062805; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i2Lbiqp5ZRvN1hAivWbYt8v17Pu/BS70LAaQOJ42RIQ=; b=kG8hu9yqAdc8Ot6CgVBnZH+iVUOEz7/lANBQnBKGr/Xv6p4TMRX6VOS8xrTBnfs6RQ TOVGMfkc8ZV+cwvk+wwlxQk1LTnHRjFlU701+sTOWa5eUBug1cibH5rJji1P4w8T8c2J QBbN41jhhakj50nG5H0vtMai/S7uuzmSwUDzO17vQiP9+1aLJ2ZdoDyMQHpmF7tVS68J TQHoVrHNXKKqLCU2WbPX87YVeS3eoQWAwOfYAF6imxW1AFqTiFv9HTe5qUwDRRDm6cje 67b7tE0DKD8w/aU2oLNUdBh11p7yM6H8XX7pF/5frL3VmUK+ZmAed21lxca4XMscyuAs X1Gg== X-Gm-Message-State: AOJu0YzWUsj3oaWbSpiXbB8iHMBwJvQlPIcX+TixOjO3L4edCTfTcP7V Ch/6dU2jLsR/7j9bSlA2CKiiyBNYOQ+yJjFjKFo= X-Received: by 2002:a17:906:f2c4:b0:a04:7e8a:dc2f with SMTP id gz4-20020a170906f2c400b00a047e8adc2fmr738405ejb.70.1701458005495; Fri, 01 Dec 2023 11:13:25 -0800 (PST) MIME-Version: 1.0 References: <20c593b6f31720a3d24d75e5e5cc3245b67249d1.camel@gmail.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 1 Dec 2023 11:13:13 -0800 Message-ID: Subject: Re: [PATCH ipsec-next v2 3/6] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro To: Daniel Xu 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, 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 morse.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 (morse.vger.email [0.0.0.0]); Fri, 01 Dec 2023 11:13:44 -0800 (PST) On Fri, Dec 1, 2023 at 11:11=E2=80=AFAM Andrii Nakryiko wrote: > > On Thu, Nov 30, 2023 at 5:33=E2=80=AFPM 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 bitf= ield > > > > writing wrapper to make the verifier happy. > > > > > > > > Two alternatives to this approach are: > > > > > > > > 1. Use the upcoming `preserve_static_offset` [0] attribute to disab= le > > > > 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 a= lso > > > > 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_rea= d.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 =3D __CORE_RELO(s, field, BYTE_SIZE); = \ > > unsigned int lshift =3D __CORE_RELO(s, field, LSHIFT_U64); = \ > > unsigned int rshift =3D __CORE_RELO(s, field, RSHIFT_U64); = \ > > - unsigned int bit_size =3D (rshift - lshift); = \ > > + unsigned int bit_size =3D (64 - rshift); = \ > > + unsigned int hi_size =3D lshift; = \ > > + unsigned int lo_size =3D (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 =3D *(unsigned int *)p; break; = \ > > case 8: val =3D *(unsigned long long *)p; break; = \ > > } = \ > > - hi =3D val >> (bit_size + rshift); = \ > > - hi <<=3D bit_size + rshift; = \ > > - lo =3D val << (bit_size + lshift); = \ > > - lo >>=3D bit_size + lshift; = \ > > + hi =3D val >> (64 - hi_size); = \ > > + hi <<=3D 64 - hi_size; = \ > > + lo =3D val << (64 - lo_size); = \ > > + lo >>=3D 64 - lo_size; = \ > > nval =3D new_val; = \ > > - nval <<=3D lshift; = \ > > - nval >>=3D rshift; = \ > > + nval <<=3D (64 - bit_size); = \ > > + nval >>=3D (64 - bit_size - lo_size); = \ > > val =3D 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 &=3D ~(bitfield_mask << shift); we can also calculate shifted mask with just bitfield_mask =3D (-1ULL) << some_left_shift >> some_right_shift; val &=3D ~bitfield_mask; > /* set bits */ > val |=3D (nval & bitfield_mask) << shift; > > ? > > > switch (byte_size) { = \ > > case 1: *(unsigned char *)p =3D val; break; = \ > > > > > > Thanks, > > Daniel