Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-06 Thread Michael Ellerman
Mark Rutland  writes:

> On Tue, Jun 05, 2018 at 08:54:03PM +1000, Michael Ellerman wrote:
>> Mark Rutland  writes:
>> > On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
>> >> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
>> >> >  /**
>> >> > + * atomic64_add_unless - add unless the number is already a given value
>> >> > + * @v: pointer of type atomic_t
>> >> > + * @a: the amount to add to v...
>> >> > + * @u: ...unless v is equal to u.
>> >> > + *
>> >> > + * Atomically adds @a to @v, so long as @v was not already @u.
>> >> > + * Returns non-zero if @v was not @u, and zero otherwise.
>> >> 
>> >> I always get confused by that wording; would something like: "Returns
>> >> true if the addition was done" not be more clear?
>> >
>> > Sounds clearer to me; I just stole the wording from the existing
>> > atomic_add_unless().
>> >
>> > I guess you'll want similar for the conditional inc/dec ops, e.g.
>> >
>> > /**
>> >  * atomic_inc_not_zero - increment unless the number is zero
>> >  * @v: pointer of type atomic_t
>> >  *
>> >  * Atomically increments @v by 1, so long as @v is non-zero.
>> >  * Returns non-zero if @v was non-zero, and zero otherwise.
>> >  */
>> 
>> If we're bike-shedding .. :)
>> 
>> I think "so long as" is overly wordy and not helpful. It'd be clearer
>> just as:
>> 
>>   * Atomically increments @v by 1, if @v is non-zero.
>
> I agree; done.

Thanks.

cheers


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-06 Thread Michael Ellerman
Mark Rutland  writes:

> On Tue, Jun 05, 2018 at 08:54:03PM +1000, Michael Ellerman wrote:
>> Mark Rutland  writes:
>> > On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
>> >> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
>> >> >  /**
>> >> > + * atomic64_add_unless - add unless the number is already a given value
>> >> > + * @v: pointer of type atomic_t
>> >> > + * @a: the amount to add to v...
>> >> > + * @u: ...unless v is equal to u.
>> >> > + *
>> >> > + * Atomically adds @a to @v, so long as @v was not already @u.
>> >> > + * Returns non-zero if @v was not @u, and zero otherwise.
>> >> 
>> >> I always get confused by that wording; would something like: "Returns
>> >> true if the addition was done" not be more clear?
>> >
>> > Sounds clearer to me; I just stole the wording from the existing
>> > atomic_add_unless().
>> >
>> > I guess you'll want similar for the conditional inc/dec ops, e.g.
>> >
>> > /**
>> >  * atomic_inc_not_zero - increment unless the number is zero
>> >  * @v: pointer of type atomic_t
>> >  *
>> >  * Atomically increments @v by 1, so long as @v is non-zero.
>> >  * Returns non-zero if @v was non-zero, and zero otherwise.
>> >  */
>> 
>> If we're bike-shedding .. :)
>> 
>> I think "so long as" is overly wordy and not helpful. It'd be clearer
>> just as:
>> 
>>   * Atomically increments @v by 1, if @v is non-zero.
>
> I agree; done.

Thanks.

cheers


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Mark Rutland
On Tue, Jun 05, 2018 at 08:54:03PM +1000, Michael Ellerman wrote:
> Mark Rutland  writes:
> > On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
> >> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
> >> >  /**
> >> > + * atomic64_add_unless - add unless the number is already a given value
> >> > + * @v: pointer of type atomic_t
> >> > + * @a: the amount to add to v...
> >> > + * @u: ...unless v is equal to u.
> >> > + *
> >> > + * Atomically adds @a to @v, so long as @v was not already @u.
> >> > + * Returns non-zero if @v was not @u, and zero otherwise.
> >> 
> >> I always get confused by that wording; would something like: "Returns
> >> true if the addition was done" not be more clear?
> >
> > Sounds clearer to me; I just stole the wording from the existing
> > atomic_add_unless().
> >
> > I guess you'll want similar for the conditional inc/dec ops, e.g.
> >
> > /**
> >  * atomic_inc_not_zero - increment unless the number is zero
> >  * @v: pointer of type atomic_t
> >  *
> >  * Atomically increments @v by 1, so long as @v is non-zero.
> >  * Returns non-zero if @v was non-zero, and zero otherwise.
> >  */
> 
> If we're bike-shedding .. :)
> 
> I think "so long as" is overly wordy and not helpful. It'd be clearer
> just as:
> 
>   * Atomically increments @v by 1, if @v is non-zero.

I agree; done.

Mark.


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Mark Rutland
On Tue, Jun 05, 2018 at 08:54:03PM +1000, Michael Ellerman wrote:
> Mark Rutland  writes:
> > On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
> >> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
> >> >  /**
> >> > + * atomic64_add_unless - add unless the number is already a given value
> >> > + * @v: pointer of type atomic_t
> >> > + * @a: the amount to add to v...
> >> > + * @u: ...unless v is equal to u.
> >> > + *
> >> > + * Atomically adds @a to @v, so long as @v was not already @u.
> >> > + * Returns non-zero if @v was not @u, and zero otherwise.
> >> 
> >> I always get confused by that wording; would something like: "Returns
> >> true if the addition was done" not be more clear?
> >
> > Sounds clearer to me; I just stole the wording from the existing
> > atomic_add_unless().
> >
> > I guess you'll want similar for the conditional inc/dec ops, e.g.
> >
> > /**
> >  * atomic_inc_not_zero - increment unless the number is zero
> >  * @v: pointer of type atomic_t
> >  *
> >  * Atomically increments @v by 1, so long as @v is non-zero.
> >  * Returns non-zero if @v was non-zero, and zero otherwise.
> >  */
> 
> If we're bike-shedding .. :)
> 
> I think "so long as" is overly wordy and not helpful. It'd be clearer
> just as:
> 
>   * Atomically increments @v by 1, if @v is non-zero.

I agree; done.

Mark.


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Michael Ellerman
Mark Rutland  writes:
> On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
>> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
>> >  /**
>> > + * atomic64_add_unless - add unless the number is already a given value
>> > + * @v: pointer of type atomic_t
>> > + * @a: the amount to add to v...
>> > + * @u: ...unless v is equal to u.
>> > + *
>> > + * Atomically adds @a to @v, so long as @v was not already @u.
>> > + * Returns non-zero if @v was not @u, and zero otherwise.
>> 
>> I always get confused by that wording; would something like: "Returns
>> true if the addition was done" not be more clear?
>
> Sounds clearer to me; I just stole the wording from the existing
> atomic_add_unless().
>
> I guess you'll want similar for the conditional inc/dec ops, e.g.
>
> /**
>  * atomic_inc_not_zero - increment unless the number is zero
>  * @v: pointer of type atomic_t
>  *
>  * Atomically increments @v by 1, so long as @v is non-zero.
>  * Returns non-zero if @v was non-zero, and zero otherwise.
>  */

If we're bike-shedding .. :)

I think "so long as" is overly wordy and not helpful. It'd be clearer
just as:

  * Atomically increments @v by 1, if @v is non-zero.

cheers


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Michael Ellerman
Mark Rutland  writes:
> On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
>> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
>> >  /**
>> > + * atomic64_add_unless - add unless the number is already a given value
>> > + * @v: pointer of type atomic_t
>> > + * @a: the amount to add to v...
>> > + * @u: ...unless v is equal to u.
>> > + *
>> > + * Atomically adds @a to @v, so long as @v was not already @u.
>> > + * Returns non-zero if @v was not @u, and zero otherwise.
>> 
>> I always get confused by that wording; would something like: "Returns
>> true if the addition was done" not be more clear?
>
> Sounds clearer to me; I just stole the wording from the existing
> atomic_add_unless().
>
> I guess you'll want similar for the conditional inc/dec ops, e.g.
>
> /**
>  * atomic_inc_not_zero - increment unless the number is zero
>  * @v: pointer of type atomic_t
>  *
>  * Atomically increments @v by 1, so long as @v is non-zero.
>  * Returns non-zero if @v was non-zero, and zero otherwise.
>  */

If we're bike-shedding .. :)

I think "so long as" is overly wordy and not helpful. It'd be clearer
just as:

  * Atomically increments @v by 1, if @v is non-zero.

cheers


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Mark Rutland
On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
> >  /**
> > + * atomic64_add_unless - add unless the number is already a given value
> > + * @v: pointer of type atomic_t
> > + * @a: the amount to add to v...
> > + * @u: ...unless v is equal to u.
> > + *
> > + * Atomically adds @a to @v, so long as @v was not already @u.
> > + * Returns non-zero if @v was not @u, and zero otherwise.
> 
> I always get confused by that wording; would something like: "Returns
> true if the addition was done" not be more clear?

Sounds clearer to me; I just stole the wording from the existing
atomic_add_unless().

I guess you'll want similar for the conditional inc/dec ops, e.g.

/**
 * atomic_inc_not_zero - increment unless the number is zero
 * @v: pointer of type atomic_t
 *
 * Atomically increments @v by 1, so long as @v is non-zero.
 * Returns non-zero if @v was non-zero, and zero otherwise.
 */

> > + */
> > +#ifdef atomic64_fetch_add_unless
> > +static inline int atomic64_add_unless(atomic64_t *v, long long a, long 
> > long u)
> 
> Do we want to make that a "bool' return?

I think so -- that's what the instrumented wrappers (and x86) do today
anyhow, and what I ended up using for the generated headers.

I'll spin a prep patch cleaning up the existing fallbacks in
, along with the comment fixup above, then rework the
additions likewise.

Thanks,
Mark.


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Mark Rutland
On Tue, Jun 05, 2018 at 11:26:37AM +0200, Peter Zijlstra wrote:
> On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
> >  /**
> > + * atomic64_add_unless - add unless the number is already a given value
> > + * @v: pointer of type atomic_t
> > + * @a: the amount to add to v...
> > + * @u: ...unless v is equal to u.
> > + *
> > + * Atomically adds @a to @v, so long as @v was not already @u.
> > + * Returns non-zero if @v was not @u, and zero otherwise.
> 
> I always get confused by that wording; would something like: "Returns
> true if the addition was done" not be more clear?

Sounds clearer to me; I just stole the wording from the existing
atomic_add_unless().

I guess you'll want similar for the conditional inc/dec ops, e.g.

/**
 * atomic_inc_not_zero - increment unless the number is zero
 * @v: pointer of type atomic_t
 *
 * Atomically increments @v by 1, so long as @v is non-zero.
 * Returns non-zero if @v was non-zero, and zero otherwise.
 */

> > + */
> > +#ifdef atomic64_fetch_add_unless
> > +static inline int atomic64_add_unless(atomic64_t *v, long long a, long 
> > long u)
> 
> Do we want to make that a "bool' return?

I think so -- that's what the instrumented wrappers (and x86) do today
anyhow, and what I ended up using for the generated headers.

I'll spin a prep patch cleaning up the existing fallbacks in
, along with the comment fixup above, then rework the
additions likewise.

Thanks,
Mark.


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Peter Zijlstra
On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
>  /**
> + * atomic64_add_unless - add unless the number is already a given value
> + * @v: pointer of type atomic_t
> + * @a: the amount to add to v...
> + * @u: ...unless v is equal to u.
> + *
> + * Atomically adds @a to @v, so long as @v was not already @u.
> + * Returns non-zero if @v was not @u, and zero otherwise.

I always get confused by that wording; would something like: "Returns
true if the addition was done" not be more clear?

> + */
> +#ifdef atomic64_fetch_add_unless
> +static inline int atomic64_add_unless(atomic64_t *v, long long a, long long 
> u)

Do we want to make that a "bool' return?

> +{
> + return atomic64_fetch_add_unless(v, a, u) != u;
> +}
> +#endif


Re: [PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-06-05 Thread Peter Zijlstra
On Tue, May 29, 2018 at 04:43:35PM +0100, Mark Rutland wrote:
>  /**
> + * atomic64_add_unless - add unless the number is already a given value
> + * @v: pointer of type atomic_t
> + * @a: the amount to add to v...
> + * @u: ...unless v is equal to u.
> + *
> + * Atomically adds @a to @v, so long as @v was not already @u.
> + * Returns non-zero if @v was not @u, and zero otherwise.

I always get confused by that wording; would something like: "Returns
true if the addition was done" not be more clear?

> + */
> +#ifdef atomic64_fetch_add_unless
> +static inline int atomic64_add_unless(atomic64_t *v, long long a, long long 
> u)

Do we want to make that a "bool' return?

> +{
> + return atomic64_fetch_add_unless(v, a, u) != u;
> +}
> +#endif


[PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-05-29 Thread Mark Rutland
Currently architecture must implement atomic_fetch_add_unless(), with
common code providing atomic_add_unless(). Architectures must also
implement atmic64_add_unless() directly, with no corresponding
atomic64_fetch_add_unless().

This divergenece is unfortunate, and means that the APIs for atomic_t,
atomic64_t, and atomic_long_t differ.

In preparation for unifying things, with architectures providing
atomic64_fetch_add_unless, this patch adds a generic
atomic64_add_unless() which will use atomic64_fetch_add_unless(). The
instrumented atomics are updated to take this case into account.

There should be no functional change as a result of this patch.

Signed-off-by: Mark Rutland 
Acked-by: Peter Zijlstra (Intel) 
Cc: Boqun Feng 
Cc: Will Deacon 
Cc: Arnd Bergmann 
Cc: Richard Henderson 
Cc: Ivan Kokshaysky 
Cc: Matt Turner 
Cc: Vineet Gupta 
Cc: Russell King 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Michael Ellerman 
Cc: Palmer Dabbelt 
Cc: Albert Ou 
---
 include/asm-generic/atomic-instrumented.h |  9 +
 include/linux/atomic.h| 16 
 2 files changed, 25 insertions(+)

diff --git a/include/asm-generic/atomic-instrumented.h 
b/include/asm-generic/atomic-instrumented.h
index 6e0818c182e2..e22d7e5f4ce7 100644
--- a/include/asm-generic/atomic-instrumented.h
+++ b/include/asm-generic/atomic-instrumented.h
@@ -93,11 +93,20 @@ static __always_inline int atomic_fetch_add_unless(atomic_t 
*v, int a, int u)
 }
 #endif
 
+#ifdef arch_atomic64_fetch_add_unless
+#define atomic64_fetch_add_unless atomic64_fetch_add_unless
+static __always_inline int atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 
u)
+{
+   kasan_check_write(v, sizeof(*v));
+   return arch_atomic64_fetch_add_unless(v, a, u);
+}
+#else
 static __always_inline bool atomic64_add_unless(atomic64_t *v, s64 a, s64 u)
 {
kasan_check_write(v, sizeof(*v));
return arch_atomic64_add_unless(v, a, u);
 }
+#endif
 
 static __always_inline void atomic_inc(atomic_t *v)
 {
diff --git a/include/linux/atomic.h b/include/linux/atomic.h
index 1105c0b37f27..8d93209052e1 100644
--- a/include/linux/atomic.h
+++ b/include/linux/atomic.h
@@ -1072,6 +1072,22 @@ static inline int atomic_dec_if_positive(atomic_t *v)
 #endif /* atomic64_try_cmpxchg */
 
 /**
+ * atomic64_add_unless - add unless the number is already a given value
+ * @v: pointer of type atomic_t
+ * @a: the amount to add to v...
+ * @u: ...unless v is equal to u.
+ *
+ * Atomically adds @a to @v, so long as @v was not already @u.
+ * Returns non-zero if @v was not @u, and zero otherwise.
+ */
+#ifdef atomic64_fetch_add_unless
+static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
+{
+   return atomic64_fetch_add_unless(v, a, u) != u;
+}
+#endif
+
+/**
  * atomic64_inc_not_zero - increment unless the number is zero
  * @v: pointer of type atomic64_t
  *
-- 
2.11.0



[PATCHv2 05/16] atomics: prepare for atomic64_fetch_add_unless()

2018-05-29 Thread Mark Rutland
Currently architecture must implement atomic_fetch_add_unless(), with
common code providing atomic_add_unless(). Architectures must also
implement atmic64_add_unless() directly, with no corresponding
atomic64_fetch_add_unless().

This divergenece is unfortunate, and means that the APIs for atomic_t,
atomic64_t, and atomic_long_t differ.

In preparation for unifying things, with architectures providing
atomic64_fetch_add_unless, this patch adds a generic
atomic64_add_unless() which will use atomic64_fetch_add_unless(). The
instrumented atomics are updated to take this case into account.

There should be no functional change as a result of this patch.

Signed-off-by: Mark Rutland 
Acked-by: Peter Zijlstra (Intel) 
Cc: Boqun Feng 
Cc: Will Deacon 
Cc: Arnd Bergmann 
Cc: Richard Henderson 
Cc: Ivan Kokshaysky 
Cc: Matt Turner 
Cc: Vineet Gupta 
Cc: Russell King 
Cc: Benjamin Herrenschmidt 
Cc: Paul Mackerras 
Cc: Michael Ellerman 
Cc: Palmer Dabbelt 
Cc: Albert Ou 
---
 include/asm-generic/atomic-instrumented.h |  9 +
 include/linux/atomic.h| 16 
 2 files changed, 25 insertions(+)

diff --git a/include/asm-generic/atomic-instrumented.h 
b/include/asm-generic/atomic-instrumented.h
index 6e0818c182e2..e22d7e5f4ce7 100644
--- a/include/asm-generic/atomic-instrumented.h
+++ b/include/asm-generic/atomic-instrumented.h
@@ -93,11 +93,20 @@ static __always_inline int atomic_fetch_add_unless(atomic_t 
*v, int a, int u)
 }
 #endif
 
+#ifdef arch_atomic64_fetch_add_unless
+#define atomic64_fetch_add_unless atomic64_fetch_add_unless
+static __always_inline int atomic64_fetch_add_unless(atomic64_t *v, s64 a, s64 
u)
+{
+   kasan_check_write(v, sizeof(*v));
+   return arch_atomic64_fetch_add_unless(v, a, u);
+}
+#else
 static __always_inline bool atomic64_add_unless(atomic64_t *v, s64 a, s64 u)
 {
kasan_check_write(v, sizeof(*v));
return arch_atomic64_add_unless(v, a, u);
 }
+#endif
 
 static __always_inline void atomic_inc(atomic_t *v)
 {
diff --git a/include/linux/atomic.h b/include/linux/atomic.h
index 1105c0b37f27..8d93209052e1 100644
--- a/include/linux/atomic.h
+++ b/include/linux/atomic.h
@@ -1072,6 +1072,22 @@ static inline int atomic_dec_if_positive(atomic_t *v)
 #endif /* atomic64_try_cmpxchg */
 
 /**
+ * atomic64_add_unless - add unless the number is already a given value
+ * @v: pointer of type atomic_t
+ * @a: the amount to add to v...
+ * @u: ...unless v is equal to u.
+ *
+ * Atomically adds @a to @v, so long as @v was not already @u.
+ * Returns non-zero if @v was not @u, and zero otherwise.
+ */
+#ifdef atomic64_fetch_add_unless
+static inline int atomic64_add_unless(atomic64_t *v, long long a, long long u)
+{
+   return atomic64_fetch_add_unless(v, a, u) != u;
+}
+#endif
+
+/**
  * atomic64_inc_not_zero - increment unless the number is zero
  * @v: pointer of type atomic64_t
  *
-- 
2.11.0