[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] x86/atomic: Fix UB on various atomic helpers
- To: "Jan Beulich" <jbeulich@xxxxxxxx>
- From: "Alejandro Vallejo" <alejandro.garciavallejo@xxxxxxx>
- Date: Wed, 14 Oct 2026 17:38:33 +0200
- Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none
- Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3LSwPFSlpzghr8qm+OHlNG5Ddb+PXeY5PcnfiGoE8vw=; b=C0fur5BdICPxZiCjeSfDWldaub7Mi4ro2z61XnJoFsKK2oUcdhfDAUb4zURudMuolAAaHHDdCTfQaByv/rtDEng5TzBa6H3jR2noHefCVPY8Y08e8nf0rc8sEcsXakbI+4uJVxvcHqxTl4HQ89EElJeXAw19xFpzfRVR1v0Pse6lfIiLt4SOvnv/SoikDzk1LXpb9o9mXGgbT2Um8iLXC9ekhSk3ysjqmmMDrZOZ4KraZvuK6BGtiR3m47g0MFqnNgBgcFPafc2o9gp8IpIp2z2TZvwd3fqs/4r1Gx8FrxfL78n/W/H2hRFjjDnI+RSOAqJgH1rdnrDTySMBNRelUw==
- Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=v/RN8EGAA7McmV7Dyz9Y+AMnhY3EHqRHFP4Ev1jbBHHvTptI61mJtStVEeci+cm06sEH4tPX9as24feY0gzwbGKBnWabB2C0ruPBUhy6AESxMtgXTEThTDxIarxGrum1wJV34LLVaEMQXts5AXYjXWxxMBwykW7LVITCvq89xtxEI5/Sse7hoyG7Q44c/dxzKEdvp4tVlEWizVkQyCnfMMq9vNJjZUAl3wmvZg/7OAUCM/Lj0Rfx3wLi5EPLbplbknhS9lB1XMhoGnz4G1rYvocT4pr+LGbCYCSeZU5BR1pdWCE1+rBGCW7nC1WyNMhmN/qRdiFQ8NIIZHNkbG9vVA==
- Authentication-results: eu.smtp.expurgate.cloud; dkim=pass header.s=selector1 header.d=amd.com header.i="@amd.com" header.h="From:Date:Subject:Message-Id:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck"
- Authentication-results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com;
- Cc: "Andrew Cooper" <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, "Teddy Astie" <teddy.astie@xxxxxxxxxx>, <xen-devel@xxxxxxxxxxxxxxxxxxxx>
- Delivery-date: Wed, 14 Oct 2026 15:38:49 +0000
- List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
On Wed Oct 14, 2026 at 4:33 PM CEST, Jan Beulich wrote:
> On 14.10.2026 15:35, Alejandro Vallejo wrote:
> > --- a/xen/arch/x86/include/asm/atomic.h
> > +++ b/xen/arch/x86/include/asm/atomic.h
> > @@ -122,7 +122,7 @@ static inline void atomic_add(int i, atomic_t *v)
> >
> > static inline int atomic_add_return(int i, atomic_t *v)
> > {
> > - return i + arch_fetch_and_add(&v->counter, i);
> > + return (unsigned int)i + arch_fetch_and_add(&v->counter, i);
> > }
>
> I'm concerned seeing this kind of change: It's merely papering over an
> issue in the caller. There's not going to be UB if the caller passes
> suitable values. And the atomic types hardly are meant to be used in
> contexts where overflow could occur.
>
> Furthermore different parts of the C spec say different things here [1],
> [2]. Hence whether overflow in signed calculations is undefined or
> implementation-defined doesn't become entirely clear, according to my
> reading.
I won't dwell on interpreting the standard, because I only care how
GCC and Clang developers interpreted it; and that's a hard trap on
signed overflow with sanitisers enabled.
I can live without this patch (it's just paranoia), but thinking about
it some more, perhaps we ought to pass -fno-strict-overflow, like Linux
does, to the compiler. This brings forward post-C23 overflow semantics
and removes this whole class of UB wholesale. Here and elsewhere.
Thoughts?
Cheers,
Alejandro
|