Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1419873rdb; Fri, 1 Dec 2023 16:41:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IGZm31rhl/mDlKsidTtmySB7E2F6LgtoCZdbadOQH8eBe9szpY86BosJnrKWrOjRHnxRH5h X-Received: by 2002:a05:6871:5385:b0:1fb:51e:bc17 with SMTP id hy5-20020a056871538500b001fb051ebc17mr613580oac.30.1701477687066; Fri, 01 Dec 2023 16:41:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701477687; cv=none; d=google.com; s=arc-20160816; b=iH0uQCTHVl9HWe9Ogq8Z9Ae9kOJrexS4kbk6vB5Ebn/cRz17IHNuBxnIvUBZL8JPjk WxjIW05QXotgh3M8mG29ve2I7uvYIWcr9RIlXAbqeE3G0JoO6prFzvH54GzED7TJYZk0 NTWpwF5eLb8Fub9WkzyuMQURckUraODvr/0O5weJS3FUCKTqv8Eq2ZaELw7SwESAe7IR C/hBnhA4rhsq6B8qO2lSz54uRQ4l+KphsINPRg4VFE0Sbb0I/T1XBR1KriAAfxsfrt5D D9CH86g2cAxvaLrhMnOh8o3GQW7amEA3kaTdBfj0zSaKf2PY9GQsObjqe2IOGZQRf2KX m0Zg== 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=f8GczNxJkUFx7ARxScvIumK7S61VTNrxCYttpBXDXk4=; fh=bQXPM+BPlJyJiDMQhKHSetP7bX/QGAl1jTX7+3QS6xM=; b=v65MQ1625PKgoPIhxmJHO23wIpVH1m5ZXMzhd0N6hOFQtYvYUvI1wLCTiSRkC42PXH GpW/1obhYE4jZx3DAC4jSoV9QJb5AkV9Ql5QuZJn7y/QID0UxDVX65rLGxeAZuQ21z9B T5RvOZINGbleSzzBah1WrYwdTuIljFWcNGZ0eVZq+/eFYwhz4V49aefBYyM6/4ssnEzm JEoR1Mvl6qYzuotEIA49tmZE+xv2ltyY/FZb9Jv1tvE3uRIf2RB9hJ7xVsTAA0nWFB5R LQyQcGRz1p4Q2dbqoxBeH5Uh8uO11zS5zFckPozvhSIfzh1UjcI9heYggONbtJcolqbu Jlmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=HKPafFjO; 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 iy13-20020a170903130d00b001cfb834d371si711080plb.113.2023.12.01.16.41.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:41:27 -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=HKPafFjO; 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 62570837B881; Fri, 1 Dec 2023 16:41:19 -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 S229841AbjLBAlE (ORCPT + 99 others); Fri, 1 Dec 2023 19:41:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50948 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235270AbjLBAlC (ORCPT ); Fri, 1 Dec 2023 19:41:02 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 508CA196; Fri, 1 Dec 2023 16:41:08 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id ffacd0b85a97d-332c7d4a6a7so1885959f8f.2; Fri, 01 Dec 2023 16:41:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701477667; x=1702082467; 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=f8GczNxJkUFx7ARxScvIumK7S61VTNrxCYttpBXDXk4=; b=HKPafFjOg3YTiY1ep7qC/xJRODT59zmTCFUY8Z/npul3XMp/IE8N2yv2KEHt40xykB 4C4UaKNxv7IRrS+QM/fDmbjBQpW+NvbKFoJsAdUwDvou5zz+6/02wRDGajMpCvGNujWp ZkQUAgOHKfBlnheHtrDcA4+sS8X5PuUwRocSqycjb/ycfdIFTiABSu9z4p3zRvfdzGi3 WNHQhqWl4aKgcVnXd/PZpbB6/SQNdXrz/ggK0Rfse0WDfN/+x/50xzpNRdksozMgwcDz Wuu+r0brZO6ytPb4FZo5A/uvFoxZJWX9nbCD1RUHbtZKpFRyfnC3fAyWEdF2iwS93jHu 7cmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701477667; x=1702082467; 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=f8GczNxJkUFx7ARxScvIumK7S61VTNrxCYttpBXDXk4=; b=OYP6Pc65nzDn/h6CGRdF1dPcJXOqa/NpZP8Sb0fhh6ehhke40++JEwgszU29VAZ7VQ YQo1kedYcAhjZtl/4vAcPOUIoCw4+al9T6Cqc1sqEopvNdWVZgRcJ0iN25fpUpkz8xBM +bVJPPOTEwfFlQkerjGgEFjVFq+x+hDYglV+JlEpBzyVUIc1Ikl2B7qHnIeUaGYFUQrE HdbE53RdhO4yAtJMrMBh/1R1K7u9/353IpLUVFj75fDmeIAZaPUTIIvzjT2NwSmtWTY0 EoWfzIcddOtpE6LKvh6HC3YVLt9P4KW6QUYI7p2Di5oN9JN6QlZn8hC2aNIzBJlpqNQi lkcw== X-Gm-Message-State: AOJu0YwZnnERz+JG2Y0S3+6A0XOD4kPf+0r1AC5vlSF/KvsKQwBBl4Kz hzrpmf1JxkuGoRT9d6WpenUSvsZImwokmOaz19E= X-Received: by 2002:a5d:4bd1:0:b0:332:e68b:416c with SMTP id l17-20020a5d4bd1000000b00332e68b416cmr900318wrt.26.1701477666559; Fri, 01 Dec 2023 16:41:06 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrii Nakryiko Date: Fri, 1 Dec 2023 16:40:54 -0800 Message-ID: Subject: Re: [PATCH ipsec-next v3 3/9] libbpf: Add BPF_CORE_WRITE_BITFIELD() macro To: Daniel Xu 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 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 16:41:20 -0800 (PST) On Fri, Dec 1, 2023 at 4:13=E2=80=AFPM Daniel Xu wrote: > > On Fri, Dec 01, 2023 at 03:49:30PM -0800, Andrii Nakryiko wrote: > > On Fri, Dec 1, 2023 at 12:24=E2=80=AFPM Daniel Xu wrote= : > > > > > > =3D=3D=3D Motivation =3D=3D=3D > > > > > > Similar to reading from CO-RE bitfields, we need a CO-RE aware bitfie= ld > > > 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 t= o > > > to have an inverse helper for writing. > > > > > > =3D=3D=3D Implementation details =3D=3D=3D > > > > > > 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 <<=3D __CORE_RELO(s, field, LSHIFT_U64); > > > val =3D 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 (bitfiel= d) > > > 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 bi= t. > > > > > > 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 =3D (~0ULL << rshift) >> lshift; // 1 > > > nval =3D new_val; // 2 > > > nval =3D (nval << rpad) & mask; // 3 > > > val =3D (val & ~mask) | nval; // 4 > > > > > > (1): Compute a mask where the set bits are the bitfield bits. The fir= st > > > 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_r= ead.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 =3D (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); = \ > > > + 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 rpad =3D rshift - lshift; = \ > > > + unsigned long long nval, mask, val; = \ > > > + = \ > > > + asm volatile("" : "+r"(p)); = \ > > > + = \ > > > + switch (byte_size) { = \ > > > + case 1: val =3D *(unsigned char *)p; break; = \ > > > + case 2: val =3D *(unsigned short *)p; break; = \ > > > + case 4: val =3D *(unsigned int *)p; break; = \ > > > + case 8: val =3D *(unsigned long long *)p; break; = \ > > > + } = \ > > > + = \ > > > + mask =3D (~0ULL << rshift) >> lshift; = \ > > > + nval =3D new_val; = \ > > > + nval =3D (nval << rpad) & mask; = \ > > > + val =3D (val & ~mask) | nval; = \ > > > > I'd simplify it to not need nval at all > > > > val =3D (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 =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 long long mask, val, nval =3D new_val; = \ > unsigned int rpad =3D rshift - lshift; = \ > - unsigned long long nval, mask, val; \ > \ > asm volatile("" : "+r"(p)); \ > \ > @@ -133,9 +133,7 @@ enum bpf_enum_value_kind { > } \ > \ > mask =3D (~0ULL << rshift) >> lshift; = \ > - nval =3D new_val; = \ > - nval =3D (nval << rpad) & mask; = \ > - val =3D (val & ~mask) | nval; = \ > + val =3D (val & ~mask) | ((nval << rpad) & mask); = \ sgtm > \ > switch (byte_size) { \ > case 1: *(unsigned char *)p =3D val; break; = \ > > > Thanks, > Daniel