Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1277174rdb; Fri, 1 Dec 2023 11:31:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IEEu+X3+ijGjNCEfDNQYgZLvDXWOBXfPO0tids/EySzNRwkmkaT5kFuvCpHVkJr1eYNfNXS X-Received: by 2002:a05:6a00:2e08:b0:6be:5367:2131 with SMTP id fc8-20020a056a002e0800b006be53672131mr53053pfb.24.1701457947623; Fri, 01 Dec 2023 11:12:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701457947; cv=none; d=google.com; s=arc-20160816; b=PEwpAj1axDc+AMoRuFbQovKHDdAI0IOVMr42d1jY4lPm9YaZ7x6B57ryWyJFoOkMpj 57n0XrwOrJfjyvCJlME8q4k80EEnyRAjiJwEWeTPzWeme9hdfB0Ad5cAVfUSjTiI0KX4 daVA0VVs6YIC1Xol+O3WYOmiaNOdrhs5XI14wcjyhPgO5ArVYBHonKvWa6D4aYkX5jrv K0BEx2KWrDxaeIjgifXX4a3uTB9Es4UwKV7P06YavaEv3hGn7YQgBnZlFYp5XjGG2HBF AnB8JniOR3sbpkJvyGyre+7XZmGGzLLpEzRPcLy0FUQOVPN8taNeJNPx61g10WvyHP78 qzSQ== 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=7b2qv0QWPtpd3SOyHqmLbQnb6QDi1Dy0b3kkXbjYsP4=; fh=fNrwRB0Z9fX+Q8B04IhS4mFwzyepgmKKkf3s70lwA6c=; b=Xkk47P1HQJHlg60pYpsraWKRD06vTUwCWL3oMQZC6GGVSqztY4eUrnwW2rnOIo8TzR 1qH6cqNceJIPgISC4YljA8E1GYTvRq26FDnv6n06rNGArXbbfPLunXcCGnOnuW86Mr4T eco/NrpVgyzjOGJSgmvFQ4si4HTfQVxK9D+D6HA08H26gE5CuM4IIh/v5pARoPSf/BZD ItG5pTZOr6t5k6KuTKjJWtQVoskaEx5quEsQermbCCy87QpycXy18qflXk+bgU1m6YiF YVBbHqoaHN1B7tpXzc+2d9H9pkgoU9L0MyDpl+sVb6rOR5hjMe9iAeBwK37GmgruU+Po UFpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=b51SJEei; 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 k34-20020a63ff22000000b005c60b583ee5si3631356pgi.848.2023.12.01.11.11.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 11:12: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=b51SJEei; 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 9FB4D832CBE3; Fri, 1 Dec 2023 11:11:55 -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 S1379446AbjLATLl (ORCPT + 99 others); Fri, 1 Dec 2023 14:11:41 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42388 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1379411AbjLATLk (ORCPT ); Fri, 1 Dec 2023 14:11:40 -0500 Received: from mail-ed1-x529.google.com (mail-ed1-x529.google.com [IPv6:2a00:1450:4864:20::529]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CD572B0; Fri, 1 Dec 2023 11:11:45 -0800 (PST) Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-54b0e553979so3164643a12.2; Fri, 01 Dec 2023 11:11:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701457904; x=1702062704; 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=7b2qv0QWPtpd3SOyHqmLbQnb6QDi1Dy0b3kkXbjYsP4=; b=b51SJEeiyQgzOMZsm5+PJonTmWBDCbNPSYxZEsQYowrVYmwZoeySw6Kb9AdQb/N4sV MlaoWKz4bMtZHozsq931bXwFl44MDmzNGskglLB/Uw4yMNS28n9HjAajCE8JEzEeure9 Q+8Ql8WX9AT1IGpc5AyuGCNK8ZYE8FlFPdOmAz/Hl63MZo6y2eluuQcpei94T3Z+A7Ir a4VaUVBo7DoiY0si5YwzgsOqPR8ZqpkWM+arwe6nofhjBwOWPsXuqF9+Pz2rJ2tFS13m UYkDCIkzQV4G1aGgPeFpt29DWjqSIt7EEsZWouGnrUF8xWhZMj5XtfoaONE3Iy17N5hp JQ8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701457904; x=1702062704; 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=7b2qv0QWPtpd3SOyHqmLbQnb6QDi1Dy0b3kkXbjYsP4=; b=lIrewYxez0NIbc76ZN8meLQFTvrIsyKf7YByaFNhCdMmcDb5WFq4enFSQB/rixOgZa VedjtQPaQp8KyJYNTvQpk599vKCh6p1VO+dWrJD4gVf8RWEVPEbzA0Og8i0R99R57S1h PdQDT+rwaEtiXpyxKktUelpyMkyLurfzKbvNREEGFJYg6pSrry/7yborwbzGIE9wvay/ NIaHOdpRZlxH6e77kHFkLSpWpC4FSG+XSw4tgrFUJP1/gXv1xfK2B+k818CbP02yJBz9 PMbyaXLWi6ZAFXtlkKVCzgOsIjUjaEpexHDN3gIVVqyaARUHRK0GFArNc6AjCY/ysBF2 dkoQ== X-Gm-Message-State: AOJu0YxEzhHzY9nDGHWLFhxFGmsXcG5Ijo53K6wRFIJmjl9BvV2PTOtg yXJjT0CiWW6yDY6X6JWrvwgsQLfQumNxLuAK3EbdVOvo X-Received: by 2002:a17:906:f84a:b0:a19:a19b:55f9 with SMTP id ks10-20020a170906f84a00b00a19a19b55f9mr1129420ejb.137.1701457903624; Fri, 01 Dec 2023 11:11:43 -0800 (PST) MIME-Version: 1.0 References: <20c593b6f31720a3d24d75e5e5cc3245b67249d1.camel@gmail.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 1 Dec 2023 11:11:31 -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:11:55 -0800 (PST) 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 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 a bit hard to use. It requires specific and > > > not-very-obvious annotations to bpftool generated vmlinux.h. It's als= o > > > 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. > > > > > > [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 =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); /* set bits */ val |=3D (nval & bitfield_mask) << shift; ? > switch (byte_size) { \ > case 1: *(unsigned char *)p =3D val; break; = \ > > > Thanks, > Daniel