From: "Alejandro Vallejo" <alejandro.garciavallejo@amd.com>
To: "Jan Beulich" <jbeulich@suse.com>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Roger Pau Monné" <roger@xenproject.org>,
"Teddy Astie" <teddy.astie@vates.tech>,
xen-devel@lists.xenproject.org
Subject: Re: [PATCH] x86/atomic: Fix UB on various atomic helpers
Date: Wed, 14 Oct 2026 17:38:33 +0200 [thread overview]
Message-ID: <DM4O9C5SOTWE.2DFLIY2LI9TQQ@amd.com> (raw)
In-Reply-To: <bfc959d3-fa71-4296-a984-4af85edaf770@suse.com>
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
prev parent reply other threads:[~2026-10-14 15:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-14 13:35 [PATCH] x86/atomic: Fix UB on various atomic helpers Alejandro Vallejo
2026-10-14 14:33 ` Jan Beulich
2026-10-14 15:38 ` Alejandro Vallejo [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DM4O9C5SOTWE.2DFLIY2LI9TQQ@amd.com \
--to=alejandro.garciavallejo@amd.com \
--cc=andrew.cooper3@citrix.com \
--cc=jbeulich@suse.com \
--cc=roger@xenproject.org \
--cc=teddy.astie@vates.tech \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.