Skip to contentSkip to conversation

Documentation/atomic_t: Clarify signed vs unsigned

PATCH Applied Peter Zijlstra 5 participants 6 messages Feb 11, 2019 – Feb 13, 2019

Subsystems (3)
lore
Share & tools
lore

Peter Zijlstra

author maintainer
#0 lore raw

PATCH 1/1 Documentation/atomic_t: Clarify signed vs unsigned

Clarify the whole signed vs unsigned issue for atomic_t.

There has been enough confusion on this topic to warrant a few explicit words I feel.

---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/atomic_t.txt +17 -0
index 913396ac5824..dca3fb0554db 100644
@@ -56,6 +56,23 @@ The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
   smp_mb__{before,after}_atomic()
 
 
+TYPES (signed vs unsigned)
+-----
+
+While atomic_t, atomic_long_t and atomic64_t use int, long and s64
+respectively (for hysterical raisins), the kernel uses -fno-strict-overflow
+(which implies -fwrapv) and defines signed overflow to behave like
+2s-complement.
+
+Therefore, an explicitly unsigned variant of the atomic ops is strictly
+unnecessary and we can simply cast, there is no UB.
+
+There was a bug in UBSAN prior to GCC-8 that would generate UB warnings for
+signed types.
+
+With this we also conform to the C/C++ _Atomic behaviour and things like
+P1236R1.
+
 
 SEMANTICS
 ---------
Acked-by on v1 1/1
#1 lore raw
Quoted 26 lines · Peter Zijlstra

Clarify the whole signed vs unsigned issue for atomic_t.

There has been enough confusion on this topic to warrant a few explicit words I feel.

---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/atomic_t.txt +7 -0
index 913396ac5824..dca3fb0554db 100644
@@ -56,6 +56,23 @@ The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
   smp_mb__{before,after}_atomic()
 
 
+TYPES (signed vs unsigned)
+-----
+
+While atomic_t, atomic_long_t and atomic64_t use int, long and s64
+respectively (for hysterical raisins), the kernel uses -fno-strict-overflow
+(which implies -fwrapv) and defines signed overflow to behave like
+2s-complement.

Documentation/atomic_t.txt The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for · line 75

Quoted 10 lines
Documentation/atomic_t.txt +10 -0
+
+Therefore, an explicitly unsigned variant of the atomic ops is strictly
+unnecessary and we can simply cast, there is no UB.
+
+There was a bug in UBSAN prior to GCC-8 that would generate UB warnings for
+signed types.
+
+With this we also conform to the C/C++ _Atomic behaviour and things like
+P1236R1.
+

Will

#2 lore raw
Quoted 31 lines · Will Deacon
Quoted 30 lines · Peter Zijlstra

Clarify the whole signed vs unsigned issue for atomic_t.

There has been enough confusion on this topic to warrant a few explicit words I feel.

---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/atomic_t.txt +11 -0
index 913396ac5824..dca3fb0554db 100644
@@ -56,6 +56,23 @@ The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
   smp_mb__{before,after}_atomic()
 
 
+TYPES (signed vs unsigned)
+-----
+
+While atomic_t, atomic_long_t and atomic64_t use int, long and s64
+respectively (for hysterical raisins), the kernel uses -fno-strict-overflow
+(which implies -fwrapv) and defines signed overflow to behave like
+2s-complement.
+
+Therefore, an explicitly unsigned variant of the atomic ops is strictly
+unnecessary and we can simply cast, there is no UB.
+
Quoted 8 lines

Documentation/atomic_t.txt The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for · line 75

Quoted 6 lines
Documentation/atomic_t.txt +6 -0
+There was a bug in UBSAN prior to GCC-8 that would generate UB warnings for
+signed types.
+
+With this we also conform to the C/C++ _Atomic behaviour and things like
+P1236R1.
+

Queued with Will's ack, thank you both!

Thanx, Paul

Acked-by on v1 1/1
#3 lore raw
In reply to Peter Zijlstra
Quoted 7 lines · Peter Zijlstra

Clarify the whole signed vs unsigned issue for atomic_t.

There has been enough confusion on this topic to warrant a few explicit words I feel.

FWIW

Regards,
Boqun

Quoted 32 lines
---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/atomic_t.txt +17 -0
index 913396ac5824..dca3fb0554db 100644
@@ -56,6 +56,23 @@ The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
   smp_mb__{before,after}_atomic()
 
 
+TYPES (signed vs unsigned)
+-----
+
+While atomic_t, atomic_long_t and atomic64_t use int, long and s64
+respectively (for hysterical raisins), the kernel uses -fno-strict-overflow
+(which implies -fwrapv) and defines signed overflow to behave like
+2s-complement.
+
+Therefore, an explicitly unsigned variant of the atomic ops is strictly
+unnecessary and we can simply cast, there is no UB.
+
+There was a bug in UBSAN prior to GCC-8 that would generate UB warnings for
+signed types.
+
+With this we also conform to the C/C++ _Atomic behaviour and things like
+P1236R1.
+
 
 SEMANTICS
 ---------
#4 lore raw
Quoted 4 lines · Boqun Feng
Quoted 3 lines · Peter Zijlstra

Clarify the whole signed vs unsigned issue for atomic_t.

Quoted 8 lines
Quoted 4 lines

There has been enough confusion on this topic to warrant a few explicit words I feel.

FWIW

Applied, thank you!

Thanx, Paul

Quoted 35 lines

Regards,
Boqun

Quoted 32 lines
---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/atomic_t.txt +17 -0
index 913396ac5824..dca3fb0554db 100644
@@ -56,6 +56,23 @@ The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
   smp_mb__{before,after}_atomic()
 
 
+TYPES (signed vs unsigned)
+-----
+
+While atomic_t, atomic_long_t and atomic64_t use int, long and s64
+respectively (for hysterical raisins), the kernel uses -fno-strict-overflow
+(which implies -fwrapv) and defines signed overflow to behave like
+2s-complement.
+
+Therefore, an explicitly unsigned variant of the atomic ops is strictly
+unnecessary and we can simply cast, there is no UB.
+
+There was a bug in UBSAN prior to GCC-8 that would generate UB warnings for
+signed types.
+
+With this we also conform to the C/C++ _Atomic behaviour and things like
+P1236R1.
+
 
 SEMANTICS
 ---------
Acked-by on v1 1/1
#5 lore raw
In reply to Peter Zijlstra
Quoted 16 lines · Peter Zijlstra

Clarify the whole signed vs unsigned issue for atomic_t.

There has been enough confusion on this topic to warrant a few explicit words I feel.

---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
Documentation/atomic_t.txt +0 -0
index 913396ac5824..dca3fb0554db 100644

Documentation/atomic_t.txt The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for · line 59

Quoted 4 lines
Documentation/atomic_t.txt +1 -0
@@ -56,6 +56,23 @@ The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for
   smp_mb__{before,after}_atomic()
 
 
+TYPES (signed vs unsigned)

s/vs/vs.

Documentation/atomic_t.txt The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for · line 68

Quoted 9 lines
Documentation/atomic_t.txt +9 -0
+-----
+
+While atomic_t, atomic_long_t and atomic64_t use int, long and s64
+respectively (for hysterical raisins), the kernel uses -fno-strict-overflow
+(which implies -fwrapv) and defines signed overflow to behave like
+2s-complement.
+
+Therefore, an explicitly unsigned variant of the atomic ops is strictly
+unnecessary and we can simply cast, there is no UB.

s/UB/Undefined Behavior

Had to read that twice. Acronyms Seriously Suck.

Documentation/atomic_t.txt The 'full' API consists of (atomic64_ and atomic_long_ prefixes omitted for · line 74

Quoted 6 lines
Documentation/atomic_t.txt +6 -0
+
+There was a bug in UBSAN prior to GCC-8 that would generate UB warnings for
+signed types.
+
+With this we also conform to the C/C++ _Atomic behaviour and things like
+P1236R1.

Thanks,

Ingo