Documentation/atomic_t: Clarify signed vs unsigned
diff mbox series

Message ID 20190211170943.GP32477@hirez.programming.kicks-ass.net
State In Next
Commit f1887143f5984f23d2360f2efed6ef481bb41117
Headers show
Series
  • Documentation/atomic_t: Clarify signed vs unsigned
Related show

Commit Message

Peter Zijlstra Feb. 11, 2019, 5:09 p.m. UTC
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.

Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
---
 Documentation/atomic_t.txt | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

Comments

Will Deacon Feb. 11, 2019, 5:28 p.m. UTC | #1
On Mon, Feb 11, 2019 at 06:09:43PM +0100, Peter Zijlstra wrote:
> 
> 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.
> 
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  Documentation/atomic_t.txt | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt
> index 913396ac5824..dca3fb0554db 100644
> --- a/Documentation/atomic_t.txt
> +++ b/Documentation/atomic_t.txt
> @@ -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.
> +

Acked-by: Will Deacon <will.deacon@arm.com>

Will
Paul E. McKenney Feb. 11, 2019, 9:13 p.m. UTC | #2
On Mon, Feb 11, 2019 at 05:28:00PM +0000, Will Deacon wrote:
> On Mon, Feb 11, 2019 at 06:09:43PM +0100, Peter Zijlstra wrote:
> > 
> > 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.
> > 
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> > ---
> >  Documentation/atomic_t.txt | 17 +++++++++++++++++
> >  1 file changed, 17 insertions(+)
> > 
> > diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt
> > index 913396ac5824..dca3fb0554db 100644
> > --- a/Documentation/atomic_t.txt
> > +++ b/Documentation/atomic_t.txt
> > @@ -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.
> > +
> 
> Acked-by: Will Deacon <will.deacon@arm.com>

Queued with Will's ack, thank you both!

								Thanx, Paul
Boqun Feng Feb. 12, 2019, 1:03 a.m. UTC | #3
On Mon, Feb 11, 2019 at 06:09:43PM +0100, Peter Zijlstra wrote:
> 
> 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.
> 
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>

FWIW

Acked-by: Boqun Feng <boqun.feng@gmail.com>

Regards,
Boqun

> ---
>  Documentation/atomic_t.txt | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt
> index 913396ac5824..dca3fb0554db 100644
> --- a/Documentation/atomic_t.txt
> +++ b/Documentation/atomic_t.txt
> @@ -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
>  ---------
Paul E. McKenney Feb. 12, 2019, 1:29 a.m. UTC | #4
On Tue, Feb 12, 2019 at 09:03:55AM +0800, Boqun Feng wrote:
> On Mon, Feb 11, 2019 at 06:09:43PM +0100, Peter Zijlstra wrote:
> > 
> > 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.
> > 
> > Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> 
> FWIW
> 
> Acked-by: Boqun Feng <boqun.feng@gmail.com>

Applied, thank you!

							Thanx, Paul

> Regards,
> Boqun
> 
> > ---
> >  Documentation/atomic_t.txt | 17 +++++++++++++++++
> >  1 file changed, 17 insertions(+)
> > 
> > diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt
> > index 913396ac5824..dca3fb0554db 100644
> > --- a/Documentation/atomic_t.txt
> > +++ b/Documentation/atomic_t.txt
> > @@ -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
> >  ---------
Ingo Molnar Feb. 13, 2019, 7:54 a.m. UTC | #5
* Peter Zijlstra <peterz@infradead.org> wrote:

> 
> 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.
> 
> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
> ---
>  Documentation/atomic_t.txt | 17 +++++++++++++++++
>  1 file changed, 17 insertions(+)
> 
> diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt
> index 913396ac5824..dca3fb0554db 100644
> --- a/Documentation/atomic_t.txt
> +++ b/Documentation/atomic_t.txt
> @@ -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.

> +-----
> +
> +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.

> +
> +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.

Acked-by: Ingo Molnar <mingo@kernel.org>

Thanks,

	Ingo

Patch
diff mbox series

diff --git a/Documentation/atomic_t.txt b/Documentation/atomic_t.txt
index 913396ac5824..dca3fb0554db 100644
--- a/Documentation/atomic_t.txt
+++ b/Documentation/atomic_t.txt
@@ -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
 ---------