LKML Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] Documentation/atomic_t: Clarify signed vs unsigned
@ 2019-02-11 17:09 Peter Zijlstra
  2019-02-11 17:28 ` Will Deacon
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Peter Zijlstra @ 2019-02-11 17:09 UTC (permalink / raw)
  To: Will Deacon, Boqun Feng, Ingo Molnar, Paul McKenney; +Cc: linux-kernel


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Documentation/atomic_t: Clarify signed vs unsigned
  2019-02-11 17:09 [PATCH] Documentation/atomic_t: Clarify signed vs unsigned Peter Zijlstra
@ 2019-02-11 17:28 ` Will Deacon
  2019-02-11 21:13   ` Paul E. McKenney
  2019-02-12  1:03 ` Boqun Feng
  2019-02-13  7:54 ` Ingo Molnar
  2 siblings, 1 reply; 6+ messages in thread
From: Will Deacon @ 2019-02-11 17:28 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Boqun Feng, Ingo Molnar, Paul McKenney, linux-kernel

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Documentation/atomic_t: Clarify signed vs unsigned
  2019-02-11 17:28 ` Will Deacon
@ 2019-02-11 21:13   ` Paul E. McKenney
  0 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2019-02-11 21:13 UTC (permalink / raw)
  To: Will Deacon; +Cc: Peter Zijlstra, Boqun Feng, Ingo Molnar, linux-kernel

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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Documentation/atomic_t: Clarify signed vs unsigned
  2019-02-11 17:09 [PATCH] Documentation/atomic_t: Clarify signed vs unsigned Peter Zijlstra
  2019-02-11 17:28 ` Will Deacon
@ 2019-02-12  1:03 ` Boqun Feng
  2019-02-12  1:29   ` Paul E. McKenney
  2019-02-13  7:54 ` Ingo Molnar
  2 siblings, 1 reply; 6+ messages in thread
From: Boqun Feng @ 2019-02-12  1:03 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Will Deacon, Ingo Molnar, Paul McKenney, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1464 bytes --]

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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Documentation/atomic_t: Clarify signed vs unsigned
  2019-02-12  1:03 ` Boqun Feng
@ 2019-02-12  1:29   ` Paul E. McKenney
  0 siblings, 0 replies; 6+ messages in thread
From: Paul E. McKenney @ 2019-02-12  1:29 UTC (permalink / raw)
  To: Boqun Feng; +Cc: Peter Zijlstra, Will Deacon, Ingo Molnar, linux-kernel

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



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Documentation/atomic_t: Clarify signed vs unsigned
  2019-02-11 17:09 [PATCH] Documentation/atomic_t: Clarify signed vs unsigned Peter Zijlstra
  2019-02-11 17:28 ` Will Deacon
  2019-02-12  1:03 ` Boqun Feng
@ 2019-02-13  7:54 ` Ingo Molnar
  2 siblings, 0 replies; 6+ messages in thread
From: Ingo Molnar @ 2019-02-13  7:54 UTC (permalink / raw)
  To: Peter Zijlstra; +Cc: Will Deacon, Boqun Feng, Paul McKenney, linux-kernel


* 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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-11 17:09 [PATCH] Documentation/atomic_t: Clarify signed vs unsigned Peter Zijlstra
2019-02-11 17:28 ` Will Deacon
2019-02-11 21:13   ` Paul E. McKenney
2019-02-12  1:03 ` Boqun Feng
2019-02-12  1:29   ` Paul E. McKenney
2019-02-13  7:54 ` Ingo Molnar

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org linux-kernel@archiver.kernel.org
	public-inbox-index lkml


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox