From: Hans-Christian Egtvedt <egtvedt@samfundet.no>
To: Peter Zijlstra <peterz@infradead.org>
Cc: linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org,
torvalds@linux-foundation.org, akpm@linux-foundation.org,
mingo@kernel.org, will.deacon@arm.com,
paulmck@linux.vnet.ibm.com,
Haavard Skinnemoen <hskinnemoen@gmail.com>
Subject: Re: [PATCH 06/20] arch,avr32: Fold atomic_ops
Date: Tue, 13 May 2014 22:40:32 +0200 [thread overview]
Message-ID: <20140513204032.GB14023@samfundet.no> (raw)
In-Reply-To: <20140509211728.GI1429@laptop.programming.kicks-ass.net>
Around Fri 09 May 2014 23:17:28 +0200 or thereabout, Peter Zijlstra wrote:
> On Fri, May 09, 2014 at 10:51:55PM +0200, Peter Zijlstra wrote:
>> On Fri, May 09, 2014 at 10:43:09PM +0200, Peter Zijlstra wrote:
>> > On Fri, May 09, 2014 at 08:32:41PM +0200, Hans-Christian Egtvedt wrote:
>> > > > - if (__builtin_constant_p(i) && (i >= -1048575) && (i <= 1048576))
>> > > > - result = atomic_sub_return(-i, v);
>> > >
>> > > I do not recall why we did it like this any more, I would assume both sub and
>> > > add to be single cycle instructions.
>>
>> Similarly, can I rip out atomic_sub_unless() ?
>
> Something like so?
>
> ---
> Subject: arch,avr32: Fold atomic_ops
> From: Peter Zijlstra <peterz@infradead.org>
> Date: Wed Apr 9 21:51:29 CEST 2014
>
> Many of the atomic op implementations are the same except for one
> instruction; fold the lot into a few CPP macros and reduce LoC.
>
> This also prepares for easy addition of new ops.
>
> Requires the asm_op because of eor.
>
> The avr32 sub instruction is limited to 21 bits, Hans-Christian cannot
> recall the reason for the weird __builtin_constant_p() tests so take all
> that out and use a straight fwd negate add instead of subtract.
Probably found the reason why we want to use sub with the signed 21-bit
limit, it uses one less register than the add instruction that can add up to
32-bit values.
Both instructions are 32-bit, to use a 16-bit instruction the immediate is
very small; 4 bit.
sub 32-bit, type IV, takes a register and subtracts a 21-bit immediate.
add 32-bit, type II, adds two register values together.
So by simplifying you loose this optimization.
> Cc: Hans-Christian Egtvedt <egtvedt@samfundet.no>
> Cc: Linus Torvalds <torvalds@linux-foundation.org>
> Cc: Haavard Skinnemoen <hskinnemoen@gmail.com>
> Signed-off-by: Peter Zijlstra <peterz@infradead.org>
> ---
> arch/avr32/include/asm/atomic.h | 145 +++++++++++++++-------------------------
> 1 file changed, 55 insertions(+), 90 deletions(-)
>
> --- a/arch/avr32/include/asm/atomic.h
> +++ b/arch/avr32/include/asm/atomic.h
> @@ -22,117 +22,84 @@
> #define atomic_read(v) (*(volatile int *)&(v)->counter)
> #define atomic_set(v, i) (((v)->counter) = i)
>
> -/*
> - * atomic_sub_return - subtract the atomic variable
> - * @i: integer value to subtract
> - * @v: pointer of type atomic_t
> - *
> - * Atomically subtracts @i from @v. Returns the resulting value.
> - */
> -static inline int atomic_sub_return(int i, atomic_t *v)
> -{
> - int result;
> -
> - asm volatile(
> - "/* atomic_sub_return */\n"
> - "1: ssrf 5\n"
> - " ld.w %0, %2\n"
> - " sub %0, %3\n"
> - " stcond %1, %0\n"
> - " brne 1b"
> - : "=&r"(result), "=o"(v->counter)
> - : "m"(v->counter), "rKs21"(i)
> - : "cc");
> -
> - return result;
> +#define ATOMIC_OP(op, asm_op) \
> +static inline void atomic_##op(int i, atomic_t *v) \
> +{ \
> + int result; \
> + \
> + asm volatile( \
> + "/* atomic_" #op " */\n" \
> + "1: ssrf 5\n" \
> + " ld.w %0, %2\n" \
> + " " #asm_op " %0, %3\n" \
> + " stcond %1, %0\n" \
> + " brne 1b" \
> + : "=&r"(result), "=o"(v->counter) \
> + : "m"(v->counter), "r"(i) \
> + : "cc"); \
> +} \
> +
> +#define ATOMIC_OP_RETURN(op, asm_op) \
> +static inline int atomic_##op##_return(int i, atomic_t *v) \
> +{ \
> + int result; \
> + \
> + asm volatile( \
> + "/* atomic_" #op "_return */\n" \
> + "1: ssrf 5\n" \
> + " ld.w %0, %2\n" \
> + " " #asm_op " %0, %3\n" \
> + " stcond %1, %0\n" \
> + " brne 1b" \
> + : "=&r"(result), "=o"(v->counter) \
> + : "m"(v->counter), "r"(i) \
> + : "cc"); \
> + \
> + return result; \
> }
>
> +#define ATOMIC_OPS(op, asm_op) \
> + ATOMIC_OP(op, asm_op) \
> + ATOMIC_OP_RETURN(op, asm_op)
> +
> +ATOMIC_OPS(add, add)
> +
> +#undef ATOMIC_OPS
> +#undef ATOMIC_OP_RETURN
> +#undef ATOMIC_OP
> +
> /*
> - * atomic_add_return - add integer to atomic variable
> - * @i: integer value to add
> - * @v: pointer of type atomic_t
> - *
> - * Atomically adds @i to @v. Returns the resulting value.
> + * The 'sub' instruction is limited to 21 bits, use negate add instead.
> */
> -static inline int atomic_add_return(int i, atomic_t *v)
> -{
> - int result;
> -
> - if (__builtin_constant_p(i) && (i >= -1048575) && (i <= 1048576))
> - result = atomic_sub_return(-i, v);
> - else
> - asm volatile(
> - "/* atomic_add_return */\n"
> - "1: ssrf 5\n"
> - " ld.w %0, %1\n"
> - " add %0, %3\n"
> - " stcond %2, %0\n"
> - " brne 1b"
> - : "=&r"(result), "=o"(v->counter)
> - : "m"(v->counter), "r"(i)
> - : "cc", "memory");
> -
> - return result;
> -}
> +#define atomic_sub(i, v) atomic_add(-(i), (v))
> +#define atomic_sub_return(i, v) atomic_add_return(-(i), (v))
>
> /*
> - * atomic_sub_unless - sub unless the number is a given value
> + * __atomic_add_unless - add unless the number is a given value
> * @v: pointer of type atomic_t
> - * @a: the amount to subtract from v...
> + * @a: the amount to add to v...
> * @u: ...unless v is equal to u.
> *
> - * Atomically subtract @a from @v, so long as it was not @u.
> + * Atomically adds @a to @v, so long as it was not @u.
> * Returns the old value of @v.
> */
> -static inline void atomic_sub_unless(atomic_t *v, int a, int u)
> +static inline int __atomic_add_unless(atomic_t *v, int a, int u)
> {
> - int tmp;
> + int tmp, old = atomic_read(v);
>
> asm volatile(
> - "/* atomic_sub_unless */\n"
> + "/* __atomic_add_unless */\n"
> "1: ssrf 5\n"
> " ld.w %0, %2\n"
> " cp.w %0, %4\n"
> " breq 1f\n"
> - " sub %0, %3\n"
> + " add %0, %3\n"
> " stcond %1, %0\n"
> " brne 1b\n"
> "1:"
> : "=&r"(tmp), "=o"(v->counter)
> - : "m"(v->counter), "rKs21"(a), "rKs21"(u)
> + : "m"(v->counter), "r"(a), "ir"(u)
> : "cc", "memory");
> -}
> -
> -/*
> - * __atomic_add_unless - add unless the number is 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 it was not @u.
> - * Returns the old value of @v.
> -*/
> -static inline int __atomic_add_unless(atomic_t *v, int a, int u)
> -{
> - int tmp, old = atomic_read(v);
> -
> - if (__builtin_constant_p(a) && (a >= -1048575) && (a <= 1048576))
> - atomic_sub_unless(v, -a, u);
> - else {
> - asm volatile(
> - "/* __atomic_add_unless */\n"
> - "1: ssrf 5\n"
> - " ld.w %0, %2\n"
> - " cp.w %0, %4\n"
> - " breq 1f\n"
> - " add %0, %3\n"
> - " stcond %1, %0\n"
> - " brne 1b\n"
> - "1:"
> - : "=&r"(tmp), "=o"(v->counter)
> - : "m"(v->counter), "r"(a), "ir"(u)
> - : "cc", "memory");
> - }
>
> return old;
> }
> @@ -168,8 +135,6 @@ static inline int atomic_sub_if_positive
> #define atomic_xchg(v, new) (xchg(&((v)->counter), new))
> #define atomic_cmpxchg(v, o, n) (cmpxchg(&((v)->counter), (o), (n)))
>
> -#define atomic_sub(i, v) (void)atomic_sub_return(i, v)
> -#define atomic_add(i, v) (void)atomic_add_return(i, v)
> #define atomic_dec(v) atomic_sub(1, (v))
> #define atomic_inc(v) atomic_add(1, (v))
>
>
--
mvh
Hans-Christian Egtvedt
next prev parent reply other threads:[~2014-05-13 20:40 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-08 13:58 [PATCH 00/20] arch atomic 'cleanup' Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 01/20] x86: Kill atomic_or_long() Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 02/20] arch,alpha: Fold atomic_ops Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 03/20] arch,arc: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-09 9:34 ` Vineet Gupta
2014-05-09 10:22 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 04/20] arch,arm: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 18:31 ` Will Deacon
2014-05-08 13:58 ` [PATCH 05/20] arch,arm64: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 18:31 ` Will Deacon
2014-05-08 13:58 ` [PATCH 06/20] arch,avr32: " Peter Zijlstra
2014-05-09 18:32 ` Hans-Christian Egtvedt
2014-05-09 20:43 ` Peter Zijlstra
2014-05-09 20:51 ` Peter Zijlstra
2014-05-09 21:17 ` Peter Zijlstra
2014-05-13 20:40 ` Hans-Christian Egtvedt [this message]
2014-05-13 20:50 ` Peter Zijlstra
2014-05-14 7:43 ` Hans-Christian Egtvedt
2014-05-31 14:14 ` Peter Zijlstra
2014-06-06 6:25 ` Hans-Christian Egtvedt
2014-05-08 13:58 ` [PATCH 07/20] arch,cris: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 15:12 ` Geert Uytterhoeven
2014-05-08 16:06 ` Peter Zijlstra
2014-05-08 17:34 ` David Miller
2014-05-08 18:17 ` Peter Zijlstra
2014-05-08 20:27 ` David Miller
2014-05-09 8:14 ` Jesper Nilsson
2014-05-08 13:58 ` [PATCH 08/20] arch,hexagon: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-12 17:28 ` rkuo
2014-05-08 13:58 ` [PATCH 09/20] arch,ia64: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 10/20] arch,m32r: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 11/20] arch,m68k: " Peter Zijlstra
2014-05-09 9:08 ` Geert Uytterhoeven
2014-05-09 9:16 ` Peter Zijlstra
2014-05-09 9:16 ` Peter Zijlstra
2014-05-09 9:44 ` Geert Uytterhoeven
2014-05-08 13:58 ` [PATCH 12/20] arch,metag: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-13 10:06 ` James Hogan
2014-05-13 10:06 ` James Hogan
2014-05-08 13:58 ` [PATCH 13/20] arch,mips: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 14/20] arch,mn10300: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 15/20] arch,parisc: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 16/20] arch,powerpc: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 17/20] arch,sh: " Peter Zijlstra
2014-05-08 13:58 ` Peter Zijlstra
2014-05-08 13:58 ` [PATCH 18/20] arch,sparc: " Peter Zijlstra
2014-05-08 13:58 ` [PATCH 19/20] arch,xtensa: " Peter Zijlstra
2014-05-08 13:59 ` [PATCH 20/20] arch: Rewrite generic atomic support Peter Zijlstra
2014-05-08 15:24 ` Sam Ravnborg
2014-05-08 18:26 ` Peter Zijlstra
2014-05-20 13:05 ` [PATCH 14/20] arch,mn10300: Fold atomic_ops David Howells
2014-05-20 13:16 ` Peter Zijlstra
2014-05-20 13:16 ` Peter Zijlstra
2014-09-24 16:54 ` [PATCH 00/20] arch atomic 'cleanup' Will Deacon
2014-09-24 16:54 ` Will Deacon
2014-09-24 18:06 ` Peter Zijlstra
2014-09-24 18:09 ` Will Deacon
2014-09-24 18:09 ` Will Deacon
2014-09-25 5:03 ` Ingo Molnar
2014-09-25 5:03 ` Ingo Molnar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20140513204032.GB14023@samfundet.no \
--to=egtvedt@samfundet.no \
--cc=akpm@linux-foundation.org \
--cc=hskinnemoen@gmail.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=paulmck@linux.vnet.ibm.com \
--cc=peterz@infradead.org \
--cc=torvalds@linux-foundation.org \
--cc=will.deacon@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).