[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] x86/atomic: Fix UB on various atomic helpers


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>
  • Date: Wed, 14 Oct 2026 15:35:50 +0200
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=lists.xenproject.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0)
  • 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=W3uZNeXE42tXk3NhU494qyzBKaz8QrbXL/HzVkz7lhM=; b=k4/EyvcPFkgnHqqJyfHiJFl8H4/E7Fhc0il5cvpTCDZJEZrcomRhgmrNaeFY9KZPX7aEOxxDiCxG5/XHXpwNfM7XVRyvkBocfyq5rPLVuuYwJS77U87i7kuGXY/VlSHXY/Q6ckKVN+ETuL7wD++TWza6cnBBNW9FCFn+MbSgiVykOKfB6zQPQUzb29S32MQ3ZslZLEJ1hLIMhMGQFF9VKGgvcgTEIxBAaFafx1xWRxo6QM/jwjNO7NyE/pEv83AoW5u7IV7mUPYok0+F1/AvmN+M/RYxsEu/NabbCntL0KZgNHITcvsRFHmjy/xhq3yyZxKyOe2KTlS8H/H9aud6uQ==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ik/n5qsDackDgQIiAfmR/UOJFDR7lQYE/08/oJ98/yfybaWkwCwxdAXbzgVIY5hVFH9elc8UmD7Chq+agMh7nsuodrFhj9lOE4ELfTBYvFgFNev/2U6LTbGYpXzMQH9Ckvm6hVJnQHdUcAM2HKa6MbXYj/QKrvfqU6ip5Vbe3nsvBZq+sqHXR8rGFuUdx4VheBQjr4oN3TlnGXsPVNwzwQ8QMdImf5/WAFqlNqRkj2ET2euG9IZ2XvB6AHOh1IOY5xq7CaroKnUIRYkGDySQwdl4C6n/wN5gpeUZyFce8OqhfJMg3ZxUm8b/Pg1lmuG6ovodWxsZIiPDmbJCBYo49A==
  • 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"
  • Cc: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>, Jan Beulich <jbeulich@xxxxxxxx>, Andrew Cooper <andrew.cooper3@xxxxxxxxxx>, Roger Pau Monné <roger@xxxxxxxxxxxxxx>, Teddy Astie <teddy.astie@xxxxxxxxxx>
  • Delivery-date: Wed, 14 Oct 2026 13:36:25 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

The atomic interface is signed. There's UB if a caller adds over INT_MAX
or subtracts below INT_MIN where the addition/subtraction is done in
C, just like in a regular int. Make the outward facing interface respect
two's complement on wraparound by round-tripping through unsigned for
arithmetic.

While at it, fix the while loop to have Xen style.

Fixes: e0efc0738163 ("atomic: replace atomic_compareandswap() with 
atomic_cmpxchg()")
Fixes: c98c80084d77 ("asm/atomic.h: implement missing and add common 
prototypes")
Signed-off-by: Alejandro Vallejo <alejandro.garciavallejo@xxxxxxx>
---
I'd prefer to turn this into an unsigned interface, but then some
callers would need changing in non-trivial ways.

OTOH, one could argue the atomics merely highlight the UB already
present in signed arithmetic, and thus this doesn't need fixing. I'd
prefer to remove UB where possible.
---
 xen/arch/x86/include/asm/atomic.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/xen/arch/x86/include/asm/atomic.h 
b/xen/arch/x86/include/asm/atomic.h
index ed4e09a503..8f227e0e6f 100644
--- 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);
 }
 
 static inline void atomic_sub(int i, atomic_t *v)
@@ -135,7 +135,9 @@ static inline void atomic_sub(int i, atomic_t *v)
 
 static inline int atomic_sub_return(int i, atomic_t *v)
 {
-    return arch_fetch_and_add(&v->counter, -i) - i;
+    unsigned int ui = i;
+
+    return arch_fetch_and_add(&v->counter, -ui) - ui;
 }
 
 static inline int atomic_sub_and_test(int i, atomic_t *v)
@@ -221,8 +223,9 @@ static inline int atomic_add_unless(atomic_t *v, int a, int 
u)
     int c, old;
 
     c = atomic_read(v);
-    while (c != u && (old = atomic_cmpxchg(v, c, c + a)) != c)
+    while ( c != u && (old = atomic_cmpxchg(v, c, (unsigned int)c + a)) != c )
         c = old;
+
     return c;
 }
 

base-commit: 7168cc02f86fb069da6e7405afbf5c7a1c215b0d
-- 
2.43.0




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.