Skip to contentSkip to conversation

use asm() for atomic_{read|set}

PATCH In review Sebastian Siewior 7 participants 13 messages Aug 14, 2007 – Aug 17, 2007 Atomic infrastructure

lore
Share & tools
lore
#0 lore raw

cover use asm() for atomic_{read|set}

I converted i386+x86-64. Compiled, booted and played for a while. The description of both patches contains the file size of four kernel builds:
- "normal" is 28e8351ac22de25034e048c680014ad824323c65 as it
- "inline asm" is with this patch
- "inline volatile" is *(volatile int *)&(v)->counter as a static inline function
- "volatile" is *(volatile int *)&(v)->counter as a #define macro

I hope I don't encourage anyone to use macros over inline functions.

Sebastian

#1 lore raw

PATCH 1/2 i386: use asm() like the other atomic operations already do.

As Segher pointed out, inline asm is better than the volatile casting all over the place. From the PowerPC patch description:
Also use inline functions instead of macros; this actually improves code generation (some code becomes a little smaller, probably because of improved alias information -- just a few hundred bytes total on a default kernel build, nothing shocking).

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux
3436201  249176  176128 3861505  3aec01 atomic_inline_volatile/vmlinux
3436203  249176  176128 3861507  3aec03 atomic_volatile/vmlinux
include/asm-i386/atomic.h +21 -6
@@ -22,19 +22,34 @@ typedef struct { int counter; } atomic_t
 /**
  * atomic_read - read atomic variable
  * @v: pointer of type atomic_t
- * 
+ *
  * Atomically reads the value of @v.
- */ 
-#define atomic_read(v)		((v)->counter)
+ */
+static __inline__ int atomic_read(const atomic_t *v)
+{
+	int t;
+
+	__asm__ __volatile__(
+			"movl %1,%0"
+			: "=r"(t)
+			: "m"(v->counter));
+	return t;
+}
 
 /**
  * atomic_set - set atomic variable
  * @v: pointer of type atomic_t
  * @i: required value
- * 
+ *
  * Atomically sets the value of @v to @i.
- */ 
-#define atomic_set(v,i)		(((v)->counter) = (i))
+ */
+static __inline__ void atomic_set(atomic_t *v, int i)
+{
+	__asm__ __volatile__(
+			"movl %1,%0"
+			: "=m"(v->counter)
+			: "ir"(i));
+}
 
 /**
  * atomic_add - add integer to atomic variable
#2 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

Quoted 5 lines

/**
* atomic_read - read atomic variable
* @v: pointer of type atomic_t
- *
+ *

Please don't change white space in patches

include/asm-i386/atomic.h typedef struct { int counter; } atomic_t · line 32

Quoted 9 lines

* Atomically reads the value of @v.
- */

include/asm-i386/atomic.h +6 -1
-#define atomic_read(v)		((v)->counter)
+ */
+static __inline__ int atomic_read(const atomic_t *v)
+{
+	int t;
+
+	__asm__ __volatile__(

And don't use __*__ in new code

-Andi

#3 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 6 lines · * Andi Kleen | 2007-08-15 02:20:35 [+0200]:
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

atomic normal is Linus' tree, commit 28e8351. Inline asm is with this patch. I wrote this in 0/2 (you want this here as well?).

Quoted 8 lines
Quoted 6 lines

/**
* atomic_read - read atomic variable
* @v: pointer of type atomic_t
- *
+ *

Please don't change white space in patches

I fixed white space errors. SubmitChecklist:24 says I should not introduce any new ones so fixing existig sounds like the right thing to do.
No white space fixing in future?

Quoted 12 lines

include/asm-i386/atomic.h typedef struct { int counter; } atomic_t · line 32

Quoted 10 lines

* Atomically reads the value of @v.
- */

include/asm-i386/atomic.h +6 -1
-#define atomic_read(v)		((v)->counter)
+ */
+static __inline__ int atomic_read(const atomic_t *v)
+{
+	int t;
+
+	__asm__ __volatile__(

And don't use __*__ in new code

Okey.

Quoted 2 lines

-Andi

Sebastian

#4 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

In reply to Andi Kleen
Quoted 6 lines · Andi Kleen
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

The inline asm stops certain optimisations from occuring.

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#5 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 9 lines · Herbert Xu
Quoted 6 lines · Andi Kleen
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

The inline asm stops certain optimisations from occuring.

Yup, the "__volatile__" after "__asm__" shouldn't be required. The previous definitions allowed the compiler to optimize certain atomic ops away, and considering we haven't seen any bugs due to the past behaviour anyway, changing it like this sounds unnecessary.

The second behavioral change that comes about here is the force- constraining of v->counter to memory in the extended asm's constraint sets. But that's required to give "volatility"-like behaviour, which I suspect is the goal of this patch.

Sebastian, could you look at the kernel images in detail and see why, how and what in the text expanded by 1158 bytes with these changes?

Quoted 3 lines

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

Me too, but I do find these more palatable than the variants using "volatile", I admit.

Satyam

#6 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 17 lines · * Satyam Sharma | 2007-08-15 18:32:10 [+0530]:
Quoted 9 lines · Herbert Xu
Quoted 6 lines · Andi Kleen
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

The inline asm stops certain optimisations from occuring.

Yup, the "__volatile__" after "__asm__" shouldn't be required. The previous definitions allowed the compiler to optimize certain atomic ops away, and considering we haven't seen any bugs due to the past behaviour anyway, changing it like this sounds unnecessary.

Quoted 4 lines

The second behavioral change that comes about here is the force- constraining of v->counter to memory in the extended asm's constraint sets. But that's required to give "volatility"-like behaviour, which I suspect is the goal of this patch.

exactly.

Quoted 2 lines

Sebastian, could you look at the kernel images in detail and see why, how and what in the text expanded by 1158 bytes with these changes?

Wow, easy. "normal" is Linus' git. It has no volatile or anything. I suspect the compiler to "optimize" some of the reads and sets away (which remain after my patch is applied).
Anyway, I tried to find out the source of this and it is not that easy. If the text is getting bigger, all references to the data section are changed and I can't simply diff. Than I searched for two "equal" .o files which differ in size. This was not better because gcc reorganized the whole .o file and the functions that were at the beginning in the first file, were at a different position in the second one. For those who have plenty of time in their hands, I posted the four kernels [1] and my four O= folders [2].
Once I have some time, I try to diff the specific function within the .o file which used the atomic_{read|set} operation.

Quoted 6 lines
Quoted 3 lines

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

Me too, but I do find these more palatable than the variants using "volatile", I admit.

Yes. The inline asm is even smaller than the volatile ones, what sounds like better optimization by the gcc (and it does exactly what volatile was used for).

[1] http://download.breakpoint.cc/kernel_builds_vmlinux.tbz2 8.3 MiB
[2] http://download.breakpoint.cc/kernel_builds.tbz2 117 MiB

Quoted 2 lines

Satyam

Sebastian

#7 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

In reply to Herbert Xu
Quoted 5 lines · Herbert Xu
Quoted 4 lines · Andi Kleen
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):
text data bss dec hex filename

3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux
Quoted 7 lines
Quoted 1 line

What is the difference between atomic_normal and atomic_inlineasm?

The inline asm stops certain optimisations from occuring.

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

There's plenty of kernel code that *wants* this though. If we can reduce the need for register-clobbering barriers, shrink our binaries, shrink our code, improve performance, and avoid heisenbugs, I think it's a win, whether or not we *need* it.

-- Chris

#8 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 8 lines · Chris Snook
Quoted 7 lines · Herbert Xu
Quoted 5 lines · Andi Kleen
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):

 text    data     bss     dec     hex filename
3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

Quoted 7 lines
Quoted 5 lines

The inline asm stops certain optimisations from occuring.

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

There's plenty of kernel code that *wants* this though. If we can

You keep saying this yet everytime I ask for an example I get nothing.

Quoted 3 lines

reduce the need for register-clobbering barriers, shrink our binaries, shrink our code, improve performance, and avoid heisenbugs, I think it's a win, whether or not we *need* it.

Hmm, you're increasing our binary size and probably killing performance.

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#9 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 8 lines · Herbert Xu
Quoted 6 lines · Chris Snook
Quoted 3 lines · Herbert Xu

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

Quoted 5 lines
Quoted 1 line

There's plenty of kernel code that *wants* this though. If we can

You keep saying this yet everytime I ask for an example I get nothing.

Agreed. Simplest thing to do will be to post those patches which eliminate the register-clobbering barriers, shrink binaries, etc. for all that code that wants this.

Signature · 1 line
SUSE Labs, Novell Inc.
#10 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

In reply to Herbert Xu
Quoted 10 lines · Herbert Xu
Quoted 9 lines · Chris Snook
Quoted 8 lines · Herbert Xu
Quoted 5 lines · Andi Kleen
Quoted 4 lines

My config with march=pentium-m and gcc (GCC) 4.1.2 (Gentoo 4.1.2):

 text    data     bss     dec     hex filename
3434150  249176  176128 3859454  3ae3fe atomic_normal/vmlinux
3435308  249176  176128 3860612  3ae884 atomic_inlineasm/vmlinux

What is the difference between atomic_normal and atomic_inlineasm?

The inline asm stops certain optimisations from occuring.

Quoted 7 lines
Quoted 4 lines
Quoted 3 lines

I'm still unconvinced why we need this because nobody has brought up any examples of kernel code that legitimately need this.

There's plenty of kernel code that *wants* this though. If we can

You keep saying this yet everytime I ask for an example I get nothing.

Just look for all the code (and there's an immense amount) that has a barrier() between two atomic_* operations, or in a loop with such operations. With these patches merged, we can proceed to *remove* those barriers.

Quoted 4 lines
Quoted 1 line

reduce the need for register-clobbering barriers, shrink our binaries, shrink our code, improve performance, and avoid heisenbugs, I think it's a win, whether or not we *need* it.

Hmm, you're increasing our binary size and probably killing performance.

The cost of these patches themselves is negligible, since people pretty much always use barriers in places where it would matter. The long-term benefit is that with guaranteed volatile behavior, we can go through and remove a whole bunch of these barriers.

-- Chris

#11 lore raw

Re: [patch 1/2] i386: use asm() like the other atomic operations already do.

Quoted 8 lines · Chris Snook
Quoted 3 lines

You keep saying this yet everytime I ask for an example I get nothing.

Just look for all the code (and there's an immense amount) that has a barrier() between two atomic_* operations, or in a loop with such operations. With these patches merged, we can proceed to *remove* those barriers.

When I say example I don't mean some hypothetical case, please give actual function/file names.

Thanks,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#12 lore raw

PATCH 2/2 x86_64: use asm() like the other atomic operations already do.

In reply to Sebastian Siewior

As Segher pointed out, inline asm is better than the volatile casting all over the place. From the PowerPC patch description:
Also use inline functions instead of macros; this actually improves code generation (some code becomes a little smaller, probably because of improved alias information -- just a few hundred bytes total on a default kernel build, nothing shocking).

My config with march=k8 and gcc (GCC) 4.1.2 (Gentoo 4.1.2):

  text    data     bss     dec     hex filename
  4002473  385936  474440 4862849  4a3381 atomic_normal/vmlinux
  4002587  385936  474440 4862963  4a33f3 atomic_inlineasm/vmlinux
  4003911  385936  474440 4864287  4a391f atomic_volatile/vmlinux
  4003959  385936  474440 4864335  4a394f atomic_volatile_inline/vmlinux
include/asm-x86_64/atomic.h +39 -9
@@ -29,19 +29,34 @@ typedef struct { int counter; } atomic_t
 /**
  * atomic_read - read atomic variable
  * @v: pointer of type atomic_t
- * 
+ *
  * Atomically reads the value of @v.
- */ 
-#define atomic_read(v)		((v)->counter)
+ */
+static __inline__ int atomic_read(const atomic_t *v)
+{
+	int t;
+
+	__asm__ __volatile__(
+			"movl %1, %0"
+			: "=r"(t)
+			: "m"(v->counter));
+	return t;
+}
 
 /**
  * atomic_set - set atomic variable
  * @v: pointer of type atomic_t
  * @i: required value
- * 
+ *
  * Atomically sets the value of @v to @i.
- */ 
-#define atomic_set(v,i)		(((v)->counter) = (i))
+ */
+static __inline__ void atomic_set(atomic_t *v, int i)
+{
+	__asm__ __volatile__(
+			"movl %1, %0"
+			: "=m"(v->counter)
+			: "ir"(i));
+}
 
 /**
  * atomic_add - add integer to atomic variable
@@ -206,7 +221,7 @@ static __inline__ int atomic_sub_return(
 
 /* An 64bit atomic type */
 
-typedef struct { volatile long counter; } atomic64_t;
+typedef struct { long counter; } atomic64_t;
 
 #define ATOMIC64_INIT(i)	{ (i) }
 
@@ -217,7 +232,16 @@ typedef struct { volatile long counter; 
  * Atomically reads the value of @v.
  * Doesn't imply a read memory barrier.
  */
-#define atomic64_read(v)		((v)->counter)
+static __inline__ long atomic64_read(const atomic64_t *v)
+{
+	long t;
+
+	__asm__ __volatile__(
+			"movq %1, %0"
+			: "=r"(t)
+			: "m"(v->counter));
+	return t;
+}
 
 /**
  * atomic64_set - set atomic64 variable
@@ -226,7 +250,13 @@ typedef struct { volatile long counter; 
  *
  * Atomically sets the value of @v to @i.
  */
-#define atomic64_set(v,i)		(((v)->counter) = (i))
+static __inline__ void atomic64_set(atomic64_t *v, long i)
+{
+	__asm__ __volatile__(
+			"movq %1, %0"
+			: "=m"(v->counter)
+			: "ir"(i));
+}
 
 /**
  * atomic64_add - add integer to atomic64 variable