Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp2733585rdh; Sun, 26 Nov 2023 17:54:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IHsEP7Hl1nhgcz6OKiD4m6w8HGmGIQdO9Pr2wCHYTq9eAqzuTLx2hgAbhXuw6F/LSWaj9eO X-Received: by 2002:a05:6808:14d0:b0:3b8:4406:c63 with SMTP id f16-20020a05680814d000b003b844060c63mr11631062oiw.41.1701050067586; Sun, 26 Nov 2023 17:54:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701050067; cv=none; d=google.com; s=arc-20160816; b=dAPp9Iz3aXIe1A1hGV5Vqw499TBt0Cgka0BxxKw63MLpd+cNtcZ3NLJcYspgCwU43X dvdNQQWU2hS9GG3srw9gXUsgwNHtCtvamFrCbzosW5WxcCoChw05oWVDOo6/4ZY3sfQ2 GPslqaDdiMcNdjR3Czt0PxMS1BDbi7TZFc1GqpBwTrNoOwedX2BAZ/e/NtL2eiipElp0 qXmte+JzoS91XRheuWH00cRpvE2QmEqKb2cYivxxfUbe3IhFt/5eDF8JWD9YFFRPXJMH vPbtUnY5dHV3jJUIMCjvFpaTJ7gIEw0fY+ptekWuDtDU/OSK8XCjvCIinRSRdst8HoO8 uNYQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent :content-transfer-encoding:autocrypt:references:in-reply-to:date:cc :to:from:subject:message-id:dkim-signature; bh=pDa4h87tLl9seJwBOHZP7XV4Q6UZXGRUr4YEgiSl+xQ=; fh=NBDObyqvO0EtNAXBSNyOar4ThuNapxe6C2X16j89Ou8=; b=jtSQ2On9yEB+oLNPT66gZXphkn+NDgnmI8d/kAwSUQ/3mfWVCcpoPd39EUb8LMckia Yg9NCxTgYDzZpKpKAg1yiBnJqoG60zKRVwt4cEkhkglQy9WdfWtXv/wO3J9Z/Sxfg8oF I84hc+XptcYAHiWhymvznJKWGI+ee7ycKOhOO1x8FQhoPYZH1lsfjfAOC/hKWyMJwYn2 r1BYK6nX5HlIXpAYoaoogFY5UzQjlfd2JDsG50OVlJPPzY9nyMkmEpQyztH3BoS581M1 oQOOHEjGnIjSN+Wp0KmUzIdUpMof8AexUJOha7XDzk7hWKG6l1hZaGbkmaPxSZZlekGy u1+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=f1bXFA6W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id k6-20020a6568c6000000b005a9f776c57dsi8524759pgt.644.2023.11.26.17.54.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 17:54:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=f1bXFA6W; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 4059A806AFD8; Sun, 26 Nov 2023 17:54:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229502AbjK0Bw4 (ORCPT + 99 others); Sun, 26 Nov 2023 20:52:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229379AbjK0Bwz (ORCPT ); Sun, 26 Nov 2023 20:52:55 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2AA83CB; Sun, 26 Nov 2023 17:53:01 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3316a4bc37dso2385638f8f.2; Sun, 26 Nov 2023 17:53:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1701049979; x=1701654779; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to; bh=pDa4h87tLl9seJwBOHZP7XV4Q6UZXGRUr4YEgiSl+xQ=; b=f1bXFA6W7vf3x7O6rQPra0ervbbnuXaktdujXFci5mINJ6kKrdq4i7f0d7BweYqVWP u71rrUw2kUrgZug9JHR6DEXr8QwCdyr4aar4qVlRRH+0bMOpm5bYJaL6zuGXm5EmpCFB TXkwpQoDTNqZky7b05OBgy4QyQGeCiiQ/feQk6sos0cMOgeSj17H2tXFIWUJk9bW394y S0rKp2yCtwEF2HxikAtXY+Cp3IpwDIPUwffrvZc+mosLP3IOx0w30uxk1TBQqUAdMO+1 aX6HZ/3GaDM3vzjCV7agGZ4uyMtoIS6v41zpwWAsBn4u2BV9GZwWpYnTnlBQYP0pxu3s tHXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701049979; x=1701654779; h=mime-version:user-agent:content-transfer-encoding:autocrypt :references:in-reply-to:date:cc:to:from:subject:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pDa4h87tLl9seJwBOHZP7XV4Q6UZXGRUr4YEgiSl+xQ=; b=n+Woeoxhsu5HsntmQeJKply06PS74SzZNMYWKr5YNqasOddY8/9o+d3Sle+e+bWkQK vC2e8nsKvjNPiY1Es9hKxA87NjVZCdVH1aFlAw55MSgSFauNmBaX06zPtbhO13aXnttq 0bUQNClm6iKu3mXD8iKHMSC/qLalfBEdIZILQdOCaMVmKD/pCqB/g6XlOVWe7cXul5Af wIqOqpBKMPf1YrNoJok15CGtejoR6OTaLfYAaS7fJzr4OeDJ8KB21ELCrD+1TY/UMMV/ EHLo6s/0uWQ3JckSY/CZdF3dP/mjw/00LPV+ulq4wDhdDevNnAsj6W04dn7t/C5lRfw9 fDOg== X-Gm-Message-State: AOJu0Yzusb6j1i5zp97sqvjB9F3deQzh2A+2/Yzwvd2F8E1uM313lMmV m4ZEOsR+IsHDbLV5LlDznZ8= X-Received: by 2002:a05:6000:239:b0:332:e62e:f0ba with SMTP id l25-20020a056000023900b00332e62ef0bamr5949115wrz.18.1701049979323; Sun, 26 Nov 2023 17:52:59 -0800 (PST) Received: from [192.168.1.95] (host-176-36-0-241.b024.la.net.ua. [176.36.0.241]) by smtp.gmail.com with ESMTPSA id w3-20020a5d5443000000b003140f47224csm10496423wrv.15.2023.11.26.17.52.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 Nov 2023 17:52:58 -0800 (PST) Message-ID: <0535eb913f1a0c2d3c291478fde07e0aa2b333f1.camel@gmail.com> Subject: Re: [PATCH ipsec-next v1 6/7] bpf: selftests: test_tunnel: Disable CO-RE relocations From: Eduard Zingerman To: Daniel Xu Cc: Yonghong Song , Alexei Starovoitov , Shuah Khan , Daniel Borkmann , Andrii Nakryiko , Alexei Starovoitov , Steffen Klassert , antony.antony@secunet.com, Mykola Lysenko , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa , bpf , "open list:KERNEL SELFTEST FRAMEWORK" , LKML , devel@linux-ipsec.org, Network Development Date: Mon, 27 Nov 2023 03:52:56 +0200 In-Reply-To: References: <391d524c496acc97a8801d8bea80976f58485810.1700676682.git.dxu@dxuuu.xyz> <0f210cef-c6e9-41c1-9ba8-225f046435e5@linux.dev> <3ec6c068-7f95-419a-a0ae-a901f95e4838@linux.dev> <18e43cdf65e7ba0d8f6912364fbc5b08a6928b35.camel@gmail.com> Autocrypt: addr=eddyz87@gmail.com; prefer-encrypt=mutual; keydata=mQGNBGKNNQEBDACwcUNXZOGTzn4rr7Sd18SA5Wv0Wna/ONE0ZwZEx+sIjyGrPOIhR14/DsOr3ZJer9UJ/WAJwbxOBj6E5Y2iF7grehljNbLr/jMjzPJ+hJpfOEAb5xjCB8xIqDoric1WRcCaRB+tDSk7jcsIIiMish0diTK3qTdu4MB6i/sh4aeFs2nifkNi3LdBuk8Xnk+RJHRoKFJ+C+EoSmQPuDQIRaF9N2m4yO0eG36N8jLwvUXnZzGvHkphoQ9ztbRJp58oh6xT7uH62m98OHbsVgzYKvHyBu/IU2ku5kVG9pLrFp25xfD4YdlMMkJH6l+jk+cpY0cvMTS1b6/g+1fyPM+uzD8Wy+9LtZ4PHwLZX+t4ONb/48i5AKq/jSsb5HWdciLuKEwlMyFAihZamZpEj+9n91NLPX4n7XeThXHaEvaeVVl4hfW/1Qsao7l1YjU/NCHuLaDeH4U1P59bagjwo9d1n5/PESeuD4QJFNqW+zkmE4tmyTZ6bPV6T5xdDRHeiITGc00AEQEAAbQkRWR1YXJkIFppbmdlcm1hbiA8ZWRkeXo4N0BnbWFpbC5jb20+iQHUBBMBCgA+FiEEx+6LrjApQyqnXCYELgxleklgRAkFAmKNNQECGwMFCQPCZwAFCwkIBwIGFQoJCAsCBBYCAwECHgECF4AACgkQLgxleklgRAlWZAv/cJ5v3zlEyP0/jMKQBqbVCCHTirPEw+nqxbkeSO6r2FUds0NnGA9a6NPOpBH+qW7a6+n6q3sIbvH7jlss4pzLI7LYlDC6z+egTv7KR5X1xFrY1uR5UGs1beAjnzYeV2hK4yqRUfygsT0Wk5e4FiNBv4+DUZ8r0cNDk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OrPSFJkt/z4acEbOQDQs8aUqrd46PV jsdqGvKnXZxzylux29UTNby4jTlz9pNJM+wPrDRmGfchLDUmf6CffaUYCbu4FiId+9+dcTCDvxbABRy1C3OJ8QY7cxfJ+pEZW18fRJ0XCl/fiV/ecAOfB3HsqgTzAn555h0rkFgay0hAvMU/mAW/CFNSIxV397zm749ZNLA0L2dMy1AKuOqH+/B+/ImBfJMDjmdyJQ8WU/OFRuGLdqOd2oZrA1iuPIa+yUYyZkaZfz/emQwpIL1+Q4p1R/OplA4yc301AqruXXUcVDbEB+joHW3hy5FwK5t5OwTKatrSJBkydSF9zdXy98fYzGniRyRA65P0Ix/8J3BYB4edY2/w0Ip/mdYsYQljBY0A== Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.50.1 MIME-Version: 1.0 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 lipwig.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 (lipwig.vger.email [0.0.0.0]); Sun, 26 Nov 2023 17:54:23 -0800 (PST) On Sun, 2023-11-26 at 18:04 -0600, Daniel Xu wrote: [...] > > Tbh I'm not sure. This test passes with preserve_static_offset > > because it suppresses preserve_access_index. In general clang > > translates bitfield access to a set of IR statements like: > >=20 > > C: > > struct foo { > > unsigned _; > > unsigned a:1; > > ... > > }; > > ... foo->a ... > >=20 > > IR: > > %a =3D getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1 > > %bf.load =3D load i8, ptr %a, align 4 > > %bf.clear =3D and i8 %bf.load, 1 > > %bf.cast =3D zext i8 %bf.clear to i32 > >=20 > > With preserve_static_offset the getelementptr+load are replaced by a > > single statement which is preserved as-is till code generation, > > thus load with align 4 is preserved. > >=20 > > On the other hand, I'm not sure that clang guarantees that load or > > stores used for bitfield access would be always aligned according to > > verifier expectations. > >=20 > > I think we should check if there are some clang knobs that prevent > > generation of unaligned memory access. I'll take a look. >=20 > Is there a reason to prefer fixing in compiler? I'm not opposed to it, > but the downside to compiler fix is it takes years to propagate and > sprinkles ifdefs into the code. > > Would it be possible to have an analogue of BPF_CORE_READ_BITFIELD()? Well, the contraption below passes verification, tunnel selftest appears to work. I might have messed up some shifts in the macro, though. Still, if clang would peek unlucky BYTE_{OFFSET,SIZE} for a particular field access might be unaligned. --- diff --git a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c b/tools/t= esting/selftests/bpf/progs/test_tunnel_kern.c index 3065a716544d..41cd913ac7ff 100644 --- a/tools/testing/selftests/bpf/progs/test_tunnel_kern.c +++ b/tools/testing/selftests/bpf/progs/test_tunnel_kern.c @@ -9,6 +9,7 @@ #include "vmlinux.h" #include #include +#include #include "bpf_kfuncs.h" #include "bpf_tracing_net.h" =20 @@ -144,6 +145,38 @@ int ip6gretap_get_tunnel(struct __sk_buff *skb) return TC_ACT_OK; } =20 +#define BPF_CORE_WRITE_BITFIELD(s, field, new_val) ({ \ + void *p =3D (void *)s + __CORE_RELO(s, field, BYTE_OFFSET); \ + unsigned byte_size =3D __CORE_RELO(s, field, BYTE_SIZE); \ + unsigned lshift =3D __CORE_RELO(s, field, LSHIFT_U64); \ + unsigned rshift =3D __CORE_RELO(s, field, RSHIFT_U64); \ + unsigned bit_size =3D (rshift - lshift); \ + unsigned long long nval, val, hi, lo; \ + \ + asm volatile("" : "=3Dr"(p) : "0"(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; \ + } \ + hi =3D val >> (bit_size + rshift); \ + hi <<=3D bit_size + rshift; \ + lo =3D val << (bit_size + lshift); \ + lo >>=3D bit_size + lshift; \ + nval =3D new_val; \ + nval <<=3D lshift; \ + nval >>=3D rshift; \ + val =3D hi | nval | lo; \ + 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; \ + } \ +}) + SEC("tc") int erspan_set_tunnel(struct __sk_buff *skb) { @@ -173,9 +206,9 @@ int erspan_set_tunnel(struct __sk_buff *skb) __u8 hwid =3D 7; =20 md.version =3D 2; - md.u.md2.dir =3D direction; - md.u.md2.hwid =3D hwid & 0xf; - md.u.md2.hwid_upper =3D (hwid >> 4) & 0x3; + BPF_CORE_WRITE_BITFIELD(&md.u.md2, dir, direction); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid, (hwid & 0xf)); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid_upper, (hwid >> 4) & 0x3); #endif =20 ret =3D bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); @@ -214,8 +247,9 @@ int erspan_get_tunnel(struct __sk_buff *skb) bpf_printk("\tindex %x\n", index); #else bpf_printk("\tdirection %d hwid %x timestamp %u\n", - md.u.md2.dir, - (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, + BPF_CORE_READ_BITFIELD(&md.u.md2, dir), + (BPF_CORE_READ_BITFIELD(&md.u.md2, hwid_upper) << 4) + + BPF_CORE_READ_BITFIELD(&md.u.md2, hwid), bpf_ntohl(md.u.md2.timestamp)); #endif =20 @@ -252,9 +286,9 @@ int ip4ip6erspan_set_tunnel(struct __sk_buff *skb) __u8 hwid =3D 17; =20 md.version =3D 2; - md.u.md2.dir =3D direction; - md.u.md2.hwid =3D hwid & 0xf; - md.u.md2.hwid_upper =3D (hwid >> 4) & 0x3; + BPF_CORE_WRITE_BITFIELD(&md.u.md2, dir, direction); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid, (hwid & 0xf)); + BPF_CORE_WRITE_BITFIELD(&md.u.md2, hwid_upper, (hwid >> 4) & 0x3); #endif =20 ret =3D bpf_skb_set_tunnel_opt(skb, &md, sizeof(md)); @@ -294,8 +328,9 @@ int ip4ip6erspan_get_tunnel(struct __sk_buff *skb) bpf_printk("\tindex %x\n", index); #else bpf_printk("\tdirection %d hwid %x timestamp %u\n", - md.u.md2.dir, - (md.u.md2.hwid_upper << 4) + md.u.md2.hwid, + BPF_CORE_READ_BITFIELD(&md.u.md2, dir), + (BPF_CORE_READ_BITFIELD(&md.u.md2, hwid_upper) << 4) + + BPF_CORE_READ_BITFIELD(&md.u.md2, hwid), bpf_ntohl(md.u.md2.timestamp)); #endif =20