Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1398135rdb; Fri, 1 Dec 2023 15:50:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFHj5uKJ5lhRvqXd2+AuYORz8m9Pchj+iBdyG6cWZ8tKh1g9eXqJYC1eawYPo4xTb+96FVk X-Received: by 2002:a05:6870:ad07:b0:1fb:217:5203 with SMTP id nt7-20020a056870ad0700b001fb02175203mr517235oab.53.1701474610458; Fri, 01 Dec 2023 15:50:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701474610; cv=none; d=google.com; s=arc-20160816; b=QTOs47+WZZj5zn0boxbmgk6EXOHSi9JWzdZ3T2bWLF0He1Rp8a3XrowbHkrdsGbAvb LAUpAitAxtDwxGo5J62iat5bNUsc4va0oA13pNu9iTBhaJ+z3BUUt/TUlxYM92s9Nwy2 3tZYniBO2DAUdSovECrljtQrmQBbQY0t1mDlKAhRT3+wUmuNcVXAfLm2BWFGp3jYYbDH rIoppAvINoMnDAf8Kvb6INcFbQkST0n6INdHIbMePmWzdPtqLMwSVYlGtApRmAXopgo1 +wQviQmLtAk8CPHckOygTiExWEcVzALQnO0RUOrUl/UhVmYA12KW+jm+hpV4RqSbr2zz cyIA== 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=Vv48LOC3e09LzBvcT71ztOIfWllD91HEPBwZOsmIU1E=; fh=bQXPM+BPlJyJiDMQhKHSetP7bX/QGAl1jTX7+3QS6xM=; b=kfrQxwf0pJkNtmyX3hH1q98CYyXRChGen9SV0Y+QNkXidVz5ZKHmHYHSviWqX8tpT5 qve2mP0t7FUzT8xw+51N3SKC4LCMq8pQ7U8VD5n79RI8XlvTfqYtuFLnP1pc9WoHVPKQ 5n2S9TF+Ar+bYlL+9evVlIIqVIW5pWN7jivnPnTACTJEXR3xQxx/QXBIFdeiCskFVhQ2 VnbxyjgH1objq6Rfl2NbZDEbiTQeI+xRnfr0KdImsxp6C6Wgy/EYjqkSRKyxrZ1C9aaH 0FLncM9hEyhoRsPkY5bZLWdUGSjCj6psa2OoDb00Hyp/j1wvbp9+wQWI4ws3Z2wMiESN 1kfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YvSrdFwv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id v1-20020a17090a898100b00280c9b49738si4376234pjn.84.2023.12.01.15.50.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 15:50:10 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=YvSrdFwv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 0D512807F48C; Fri, 1 Dec 2023 15:50:07 -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 S230313AbjLAXtm (ORCPT + 99 others); Fri, 1 Dec 2023 18:49:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbjLAXtl (ORCPT ); Fri, 1 Dec 2023 18:49:41 -0500 Received: from mail-ej1-x62f.google.com (mail-ej1-x62f.google.com [IPv6:2a00:1450:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CA361B4; Fri, 1 Dec 2023 15:49:44 -0800 (PST) Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a196f84d217so228506366b.3; Fri, 01 Dec 2023 15:49:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701474583; x=1702079383; 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=Vv48LOC3e09LzBvcT71ztOIfWllD91HEPBwZOsmIU1E=; b=YvSrdFwvxoKxpAL+MxrLtX8J6TkR7KrH/dj81YXnhDmDJnyA1DPb9hkalFlDMgVawG hggePwDXRnUkK8aLdkRrR4GmNzZvLPBIr/yHMOt6v6N6fP+eOWPof2wqsfGgAeoJuBts Nv9vVKo7a6FakZczz229FxKGHL47ZPViztUg+59s/pTAPG7IvSDjjW9CjNgYN7qEvmkP 6/pLkht3KzQIvNUqfT/135IY1lsyY9+bpikpk8xP5wuUBf87g43sCB4k+Y0aMt3E6Aph yAfkxq+zbwyiZdz78vT6PpE6FmTy/iFOJJz1tzvKZzpdafq7e1KUqBttpn/NJSibdsu4 srvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701474583; x=1702079383; 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=Vv48LOC3e09LzBvcT71ztOIfWllD91HEPBwZOsmIU1E=; b=E5OggjkToeH78it4hyMOHDzyDJXvCshCfAENvK/TLL7WAk2XSGoJFGeflSm/W3ScOy B7EoCnYiUefq4F28SZMsrbPwcw/PPNjkzbUWp2sYCc9KoPxAHudwL7Uv+DJCLWy69Ul2 cN8k20eneEdTFYL5rIYx2wzSHOdha4oYhBLmG1hSUKuk8FBKl6my6PpFQHeHJFHk9HrG 4q4FLsvjzAekvt+wp7NlAY81a+YH9NMcxSt3vRMLdcBHJJ43GA3XjEHBBHGaF0jf9Apf FE/huZl1IQQHCLb+xF0o0t1DdUieXP/TCAIbYrwRwqroEC4MNPos4P0WY47g/OlFjdaI zv7g== X-Gm-Message-State: AOJu0Yxu/O+9qDao23efdvmNnBd61h8P6IukNk+Yzj7n8RiCOwhz4JBR SpDBNVLgEgfK4w0rzhOmV5Gax8WLh9pRgSs9mqY= X-Received: by 2002:a17:906:d0d7:b0:a19:a1ba:8cfd with SMTP id bq23-20020a170906d0d700b00a19a1ba8cfdmr1374871ejb.155.1701474582630; Fri, 01 Dec 2023 15:49:42 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Andrii Nakryiko Date: Fri, 1 Dec 2023 15:49:30 -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 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]); Fri, 01 Dec 2023 15:50:07 -0800 (PST) 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 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. > > =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? > > 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! > 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 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 =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. > + \ > + switch (byte_size) { \ > + case 1: *(unsigned char *)p =3D val; break; = \ > + case 2: *(unsigned short *)p =3D val; break; = \ > + case 4: *(unsigned int *)p =3D val; break; = \ > + case 8: *(unsigned long long *)p =3D val; break; = \ > + } \ > +}) > + > #define ___bpf_field_ref1(field) (field) > #define ___bpf_field_ref2(type, field) (((typeof(type) *)0)->field) > #define ___bpf_field_ref(args...) = \ > -- > 2.42.1 >