From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTP id 517E55A026F for ; Sat, 13 Jan 2024 23:50:39 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705186237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=S+Ia7ASOGU/qUt8THm6HZyIx3r7gjPAkPMuSpkMmYJw=; b=YP60ALAxyfxfAkrqtQp916C9/4BAMllDQzwy0J9cmKCVN3Gy0gc5AxOcTbgnhg3/XH81p9 LXjIwd95nQPUFDxqqIpbK4/8rA9OcIfozFXmlHGvAM46mZMz6A/XKiS7fpjG0n+UTxSPOd y6LJNfuJQnWf7Tzoi5TA7vWBamJiZlo= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-433-m0DLZ55cPAWpSKKDhmxFKA-1; Sat, 13 Jan 2024 17:50:36 -0500 X-MC-Unique: m0DLZ55cPAWpSKKDhmxFKA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D01C9185A781; Sat, 13 Jan 2024 22:50:35 +0000 (UTC) Received: from elisabeth (unknown [10.39.208.3]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AE5EA3C25; Sat, 13 Jan 2024 22:50:34 +0000 (UTC) Date: Sat, 13 Jan 2024 23:50:28 +0100 From: Stefano Brivio To: David Gibson Subject: Re: [PATCH 1/8] tcp: Fix address type for tcp_sock_init_af() Message-ID: <20240113235028.63592a23@elisabeth> In-Reply-To: References: <20231207143140.1851378-1-david@gibson.dropbear.id.au> <20231207143140.1851378-2-david@gibson.dropbear.id.au> <20231227212506.75eb41c7@elisabeth> <20231228111119.48afa9a5@elisabeth> Organization: Red Hat MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Message-ID-Hash: RF2Z3LKNI5R3LF5KETLUGCQB3IYW4HAE X-Message-ID-Hash: RF2Z3LKNI5R3LF5KETLUGCQB3IYW4HAE X-MailFrom: sbrivio@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Sun, 7 Jan 2024 16:35:56 +1100 David Gibson wrote: > On Thu, Dec 28, 2023 at 11:11:19AM +0100, Stefano Brivio wrote: > > On Thu, 28 Dec 2023 13:42:25 +1100 > > David Gibson wrote: > > > > > On Wed, Dec 27, 2023 at 09:25:06PM +0100, Stefano Brivio wrote: > > > > On Fri, 8 Dec 2023 01:31:33 +1100 > > > > David Gibson wrote: > > > > > > > > > This takes a struct in_addr * (i.e. an IPv4 address), although it's > > > > > explicitly supposed to handle IPv6 as well. Both its caller and sock_l4() > > > > > which it calls use a void * for the address, which can be either an in_addr > > > > > or an in6_addr. > > > > > > > > > > We get away with this, because we don't do anything with the pointer other > > > > > than transfer it from the caller to sock_l4(), but it's misleading. And > > > > > quite possibly technically UB, because C is like that. > > > > > > > > > > Signed-off-by: David Gibson > > > > > --- > > > > > tcp.c | 2 +- > > > > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > > > > > > > diff --git a/tcp.c b/tcp.c > > > > > index f506cfd..bda95b2 100644 > > > > > --- a/tcp.c > > > > > +++ b/tcp.c > > > > > @@ -2905,7 +2905,7 @@ void tcp_sock_handler(struct ctx *c, union epoll_ref ref, uint32_t events) > > > > > * Return: fd for the new listening socket, negative error code on failure > > > > > */ > > > > > static int tcp_sock_init_af(const struct ctx *c, int af, in_port_t port, > > > > > - const struct in_addr *addr, const char *ifname) > > > > > + const void *addr, const char *ifname) > > > > > > > > This is obviously correct. > > > > > > > > However, after a lot of thinking: (gcc) optimisations based on > > > > Type-Based Alias Analysis, which we don't disable on this path, could, > > > > now, happily defer filling 'addr' with inet_pton() in conf_ports() to a > > > > point *after* the tcp_sock_init() call. > > > > > > Hrm... possibly. The fact that the addr variable in conf_ports() is a > > > char array, not a struct in*_addr might save us. > > > > Hmm, look at the commit message for a48c5c2abf8a ("treewide: Disable > > gcc strict aliasing rules as needed, drop workarounds"): that didn't help > > with the checksum functions, because yes, at some point I had char *, but > > then I used those as different types. > > > > I guess struct in_addr / struct in6_addr as we have in sock_l4() might be > > equivalent to that. > > > > > I think replacing it > > > with a union of an in_addr and in6_addr would also be ok. > > > > That should work, yes, and that's what I originally wanted to suggest, > > before remembering about union inany_addr... but that doesn't fit, see > > below. > > > > > > Without this patch, at least 32 bits must be updated before the call. > > > > > > I'm not sure that's correct. If the compiler is allowed to assume > > > that a char[] and a void * aren't aliased (even if they clearly are), > > > then I'd expect it to also be allowed to assume that a char[] and a > > > struct in_addr * aren't aliased. > > > > Ouch, right, they aren't (again... sarcastically speaking). > > > > > > It might sound like a joke because... it actually is. But look at what > > > > we had to do for the functions in checksum.c. We pass const void *buf, > > > > and anything that buf points to can be updated (with TBAA) after the > > > > call. > > > > > > > > I don't see any conceptual difference between this case and those > > > > functions. > > > > > > > > Anyway, that won't reasonably happen here, and in any case this would > > > > have been broken for IPv6, so I'll go ahead and apply this. > > > > > > > > But, eventually, I think we should switch all these usages to union > > > > inany_addr *. > > > > > > So, we may be able to use union inany_addr in some places, but that's > > > not the same thing as this: inany_addr carries IPv4 addresses as > > > mapped IPv6 addresses, it's not switched on a separate af parameter. > > > > I really meant *a pointer* to union inany_addr, that is: > > > > > We could, of course, define a new type as a simple union of in_addr > > > and in6_addr. > > > > ...abusing it instead of using a separate union. On the other hand, > > given where 'a4' is in there, it's not necessarily the same for > > (strict) aliasing considerations. > > > > Is "union in10_addr" fashionable enough? We could use A [16], but it's > > inconvenient to type, and difficult to pronounce. > > Uh, I haven't heard of either of those. Right, I just made that up. I was suggesting a new name for this hypothetical union. -- Stefano