Skip to contentSkip to conversation

x86/atomic changes for v2.6.35

GIT PULL Ingo Molnar 5 participants 8 messages May 17, 2010 – May 19, 2010

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Linus,

Please pull the latest x86-atomic-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git x86-atomic-for-linus

out-of-topic modifications in x86-atomic-for-linus:
---------------------------------------------------

lib/Makefile                       # 86a8938: lib: Add self-test for atomic64_t
lib/atomic64.c                     # 9757789: lib: Fix atomic64_add_unless retu
lib/atomic64_test.c                # a5c9161: x86, atomic64: In selftest, disti
                                   # 25a304f: lib: Fix atomic64_inc_not_zero te
                                   # 9efbcd5: lib: Fix atomic64_add_unless test
                                   # d7f6de1: x86: Implement atomic[64]_dec_if_
                                   # 8f4f202: lib: Only test atomic64_dec_if_po
                                   # 86a8938: lib: Add self-test for atomic64_t

( Note, the shortlog and diffstat is relative to the
previous x86/asm tree. )

Thanks,

Ingo

------------------>
H. Peter Anvin (2):
x86, atomic64: In selftest, distinguish x86-64 from 586+
x86: Fix LOCK_PREFIX_HERE for uniprocessor build

Luca Barbieri (11):
x86: Add support for lock prefix in alternatives
x86-32: Allow UP/SMP lock replacement in cmpxchg64
lib: Add self-test for atomic64_t
x86-32: Rewrite 32-bit atomic64 functions in assembly
lib: Only test atomic64_dec_if_positive on archs having it
x86: Implement atomic[64]_dec_if_positive()
lib: Fix atomic64_add_unless test
x86-32: Fix atomic64_add_unless return value convention
lib: Fix atomic64_add_unless return value convention
lib: Fix atomic64_inc_not_zero test
x86-32: Fix atomic64_inc_not_zero return value convention

 arch/x86/include/asm/alternative.h |    9 +-
 arch/x86/include/asm/atomic.h      |   23 +++
 arch/x86/include/asm/atomic64_32.h |  278 +++++++++++++++++++++++++++---------
 arch/x86/include/asm/atomic64_64.h |   23 +++
 arch/x86/include/asm/cmpxchg_32.h  |    3 +-
 arch/x86/kernel/alternative.c      |    6 +-
 arch/x86/lib/Makefile              |    3 +-
 arch/x86/lib/atomic64_32.c         |  273 +++++++----------------------------
 arch/x86/lib/atomic64_386_32.S     |  174 ++++++++++++++++++++++
 arch/x86/lib/atomic64_cx8_32.S     |  224 +++++++++++++++++++++++++++++
 lib/Kconfig.debug                  |    7 +
 lib/Makefile                       |    2 +
 lib/atomic64.c                     |    4 +-
 lib/atomic64_test.c                |  164 +++++++++++++++++++++
 14 files changed, 895 insertions(+), 298 deletions(-)
 create mode 100644 arch/x86/lib/atomic64_386_32.S
 create mode 100644 arch/x86/lib/atomic64_cx8_32.S
 create mode 100644 lib/atomic64_test.c
arch/x86/include/asm/alternative.h +6 -3
index 714bf24..92a9033 100644
@@ -28,14 +28,17 @@
  */
 
 #ifdef CONFIG_SMP
-#define LOCK_PREFIX \
+#define LOCK_PREFIX_HERE \
 		".section .smp_locks,\"a\"\n"	\
 		".balign 4\n"			\
-		".long 661f - .\n" /* offset */	\
+		".long 671f - .\n" /* offset */	\
 		".previous\n"			\
-		"661:\n\tlock; "
+		"671:"
+
+#define LOCK_PREFIX LOCK_PREFIX_HERE "\n\tlock; "
 
 #else /* ! CONFIG_SMP */
+#define LOCK_PREFIX_HERE ""
 #define LOCK_PREFIX ""
 #endif
 
arch/x86/include/asm/atomic.h +23 -0
index 8f8217b..706c694 100644
@@ -246,6 +246,29 @@ static inline int atomic_add_unless(atomic_t *v, int a, int u)
 
 #define atomic_inc_not_zero(v) atomic_add_unless((v), 1, 0)
 
+/*
+ * atomic_dec_if_positive - decrement by 1 if old value positive
+ * @v: pointer of type atomic_t
+ *
+ * The function returns the old value of *v minus 1, even if
+ * the atomic variable, v, was not decremented.
+ */
+static inline int atomic_dec_if_positive(atomic_t *v)
+{
+	int c, old, dec;
+	c = atomic_read(v);
+	for (;;) {
+		dec = c - 1;
+		if (unlikely(dec < 0))
+			break;
+		old = atomic_cmpxchg((v), c, dec);
+		if (likely(old == c))
+			break;
+		c = old;
+	}
+	return dec;
+}
+
 /**
  * atomic_inc_short - increment of a short integer
  * @v: pointer to type int
arch/x86/include/asm/atomic64_32.h +211 -67

357 lines not on this page · raw message

arch/x86/include/asm/atomic64_64.h +23 -0
index 51c5b40..4d6e2cd 100644
@@ -221,4 +221,27 @@ static inline int atomic64_add_unless(atomic64_t *v, long a, long u)
 
 #define atomic64_inc_not_zero(v) atomic64_add_unless((v), 1, 0)
 
+/*
+ * atomic64_dec_if_positive - decrement by 1 if old value positive
+ * @v: pointer of type atomic_t
+ *
+ * The function returns the old value of *v minus 1, even if
+ * the atomic variable, v, was not decremented.
+ */
+static inline long atomic64_dec_if_positive(atomic64_t *v)
+{
+	long c, old, dec;
+	c = atomic64_read(v);
+	for (;;) {
+		dec = c - 1;
+		if (unlikely(dec < 0))
+			break;
+		old = atomic64_cmpxchg((v), c, dec);
+		if (likely(old == c))
+			break;
+		c = old;
+	}
+	return dec;
+}
+
 #endif /* _ASM_X86_ATOMIC64_64_H */
arch/x86/include/asm/cmpxchg_32.h +2 -1
index ffb9bb6..8859e12 100644
@@ -271,7 +271,8 @@ extern unsigned long long cmpxchg_486_u64(volatile void *, u64, u64);
 	__typeof__(*(ptr)) __ret;				\
 	__typeof__(*(ptr)) __old = (o);				\
 	__typeof__(*(ptr)) __new = (n);				\
-	alternative_io("call cmpxchg8b_emu",			\
+	alternative_io(LOCK_PREFIX_HERE				\
+			"call cmpxchg8b_emu",			\
 			"lock; cmpxchg8b (%%esi)" ,		\
 		       X86_FEATURE_CX8,				\
 		       "=A" (__ret),				\
arch/x86/kernel/alternative.c +4 -2
index 9367384..7023773 100644
@@ -247,7 +247,8 @@ static void alternatives_smp_lock(const s32 *start, const s32 *end,
 		if (!*poff || ptr < text || ptr >= text_end)
 			continue;
 		/* turn DS segment override prefix into lock prefix */
-		text_poke(ptr, ((unsigned char []){0xf0}), 1);
+		if (*ptr == 0x3e)
+			text_poke(ptr, ((unsigned char []){0xf0}), 1);
 	};
 	mutex_unlock(&text_mutex);
 }
@@ -267,7 +268,8 @@ static void alternatives_smp_unlock(const s32 *start, const s32 *end,
 		if (!*poff || ptr < text || ptr >= text_end)
 			continue;
 		/* turn lock prefix into DS segment override prefix */
-		text_poke(ptr, ((unsigned char []){0x3E}), 1);
+		if (*ptr == 0xf0)
+			text_poke(ptr, ((unsigned char []){0x3E}), 1);
 	};
 	mutex_unlock(&text_mutex);
 }
arch/x86/lib/Makefile +2 -1
index 419386c..3ac4a8a 100644
@@ -26,11 +26,12 @@ obj-y += msr.o msr-reg.o msr-reg-export.o
 
 ifeq ($(CONFIG_X86_32),y)
         obj-y += atomic64_32.o
+        lib-y += atomic64_cx8_32.o
         lib-y += checksum_32.o
         lib-y += strstr_32.o
         lib-y += semaphore_32.o string_32.o
 ifneq ($(CONFIG_X86_CMPXCHG64),y)
-        lib-y += cmpxchg8b_emu.o
+        lib-y += cmpxchg8b_emu.o atomic64_386_32.o
 endif
         lib-$(CONFIG_X86_USE_3DNOW) += mmx_32.o
 else
arch/x86/lib/atomic64_32.c +51 -222

276 lines not on this page · raw message

arch/x86/lib/atomic64_386_32.S new file +174 -0

174 lines not on this page · raw message

arch/x86/lib/atomic64_cx8_32.S new file +224 -0

224 lines not on this page · raw message

lib/Kconfig.debug +7 -0
index 935248b..2313840 100644
@@ -1086,6 +1086,13 @@ config DMA_API_DEBUG
 	  This option causes a performance degredation.  Use only if you want
 	  to debug device drivers. If unsure, say N.
 
+config ATOMIC64_SELFTEST
+	bool "Perform an atomic64_t self-test at boot"
+	help
+	  Enable this option to test the atomic64_t functions at boot.
+
+	  If unsure, say N.
+
 source "samples/Kconfig"
 
 source "lib/Kconfig.kgdb"
lib/Makefile +2 -0
index 0d40152..af759f3 100644
@@ -101,6 +101,8 @@ obj-$(CONFIG_GENERIC_CSUM) += checksum.o
 
 obj-$(CONFIG_GENERIC_ATOMIC64) += atomic64.o
 
+obj-$(CONFIG_ATOMIC64_SELFTEST) += atomic64_test.o
+
 hostprogs-y	:= gen_crc32table
 clean-files	:= crc32table.h
 
lib/atomic64.c +2 -2
index 8bee16e..a21c12b 100644
@@ -162,12 +162,12 @@ int atomic64_add_unless(atomic64_t *v, long long a, long long u)
 {
 	unsigned long flags;
 	spinlock_t *lock = lock_addr(v);
-	int ret = 1;
+	int ret = 0;
 
 	spin_lock_irqsave(lock, flags);
 	if (v->counter != u) {
 		v->counter += a;
-		ret = 0;
+		ret = 1;
 	}
 	spin_unlock_irqrestore(lock, flags);
 	return ret;
lib/atomic64_test.c new file +164 -0

164 lines not on this page · raw message

#1 lore raw
Quoted 5 lines · Ingo Molnar

Please pull the latest x86-atomic-for-linus git tree from:

  git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git x86-atomic-for-linus

Quoted 10 lines

out-of-topic modifications in x86-atomic-for-linus:
---------------------------------------------------
lib/Makefile                       # 86a8938: lib: Add self-test for atomic64_t lib/atomic64.c                     # 9757789: lib: Fix atomic64_add_unless retu lib/atomic64_test.c                # a5c9161: x86, atomic64: In selftest, disti # 25a304f: lib: Fix atomic64_inc_not_zero te # 9efbcd5: lib: Fix atomic64_add_unless test # d7f6de1: x86: Implement atomic[64]_dec_if_ # 8f4f202: lib: Only test atomic64_dec_if_po # 86a8938: lib: Add self-test for atomic64_t

Is having atomic64_t mandatory now?

According to the allmodconfig build logs
(http://kisskb.ellerman.id.au/kisskb/matrix/),
several architectures (at least m68k and mips) don't have it. Furthermore, the test fails to compile on a few architectures that do have it (parisc, s390, sh, ...).

<boilerplate>
It's a pity this wasn't raised/resolved between its detection in linux-next and before it entered mainline...
</boilerplate>

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 25 lines · Geert Uytterhoeven
Quoted 7 lines · Ingo Molnar

Please pull the latest x86-atomic-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git x86-atomic-for-linus

out-of-topic modifications in x86-atomic-for-linus:
---------------------------------------------------

Quoted 8 lines
lib/Makefile                       # 86a8938: lib: Add self-test for atomic64_t
lib/atomic64.c                     # 9757789: lib: Fix atomic64_add_unless retu
lib/atomic64_test.c                # a5c9161: x86, atomic64: In selftest, disti
                                  # 25a304f: lib: Fix atomic64_inc_not_zero te
                                  # 9efbcd5: lib: Fix atomic64_add_unless test
                                  # d7f6de1: x86: Implement atomic[64]_dec_if_
                                  # 8f4f202: lib: Only test atomic64_dec_if_po
                                  # 86a8938: lib: Add self-test for atomic64_t

Is having atomic64_t mandatory now?

According to the allmodconfig build logs
(http://kisskb.ellerman.id.au/kisskb/matrix/),
several architectures (at least m68k and mips) don't have it. Furthermore, the test fails to compile on a few architectures that do have it (parisc, s390, sh, ...).

Quoted 5 lines

<boilerplate>
It's a pity this wasn't raised/resolved between its detection in linux-next and before it entered mainline...
</boilerplate>

Is having atomic64_t mandatory? Not yet, I don't think, but it probably will be soon -- which is why there is a generic implementation available. All those architectures just need to select CONFIG_GENERIC_ATOMIC64 and voilà, problem solved.

As far as your boilerplate is concerned, I think Linus made it clear at the Kernel Summit that is it not the obligation of x86/ARM/PowerPC to slow down to not break the smaller architectures; it's the responsibility of those architecture maintainers to keep up. Sorry.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#3 lore raw

Hi,

Quoted 11 lines · On Wed, 19 May 2010 07:24:00 -0700 "H. Peter Anvin" <[email protected]> wrote:
Quoted 4 lines · Geert Uytterhoeven

<boilerplate>
It's a pity this wasn't raised/resolved between its detection in linux-next and before it entered mainline...
</boilerplate>

As far as your boilerplate is concerned, I think Linus made it clear at the Kernel Summit that is it not the obligation of x86/ARM/PowerPC to slow down to not break the smaller architectures; it's the responsibility of those architecture maintainers to keep up. Sorry.

I don't think this reply has anything to do with the sentiments expressed by Geert above. My interpretation of his comments is just that it is a pity noone noticed the problem while it was only in linux-next and reported it widely (like on linux-arch) so something could have been done before it all Linus' tree. There was no suggestion of slowing the pace of development.

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/
#4 lore raw
Quoted 15 lines · Stephen Rothwell

Hi,

Quoted 11 lines · On Wed, 19 May 2010 07:24:00 -0700 "H. Peter Anvin" <[email protected]> wrote:
Quoted 4 lines · Geert Uytterhoeven

<boilerplate>
It's a pity this wasn't raised/resolved between its detection in linux-next and before it entered mainline...
</boilerplate>

As far as your boilerplate is concerned, I think Linus made it clear at the Kernel Summit that is it not the obligation of x86/ARM/PowerPC to slow down to not break the smaller architectures; it's the responsibility of those architecture maintainers to keep up. Sorry.

Quoted 6 lines

I don't think this reply has anything to do with the sentiments expr by Geert above. My interpretation of his comments is just that it is a pity noone noticed the problem while it was only in linux-next and reported it widely (like on linux-arch) so something could have been done before it all Linus' tree. There was no suggestion of slowing the pace of development.

It was discussed on linux-kernel -- note that there is no breakage for smaller architectures unless you enable the test directly or via randconfig.

The other part is that generic atomic64_t has been available since middle of 2009, and was *also* discussed extensively on linux-kernel -- in fact, several of the smaller architectures added support at that time. That the breakage occurred because of an inconsequential test rather than real code is thus really nothing but fortunate.

-hpa

Signature · 2 lines
H. Peter Anvin, Intel Open Source Technology Center
I work for Intel.  I don't speak on their behalf.
#5 lore raw
Quoted 3 lines · On Wed, 19 May 2010 08:01:35 -0700 "H. Peter Anvin" <[email protected]> wrote:

It was discussed on linux-kernel -- note that there is no breakage for smaller architectures unless you enable the test directly or via randconfig.

or allmodconfig or allyesconfig.

Quoted 5 lines

The other part is that generic atomic64_t has been available since middle of 2009, and was *also* discussed extensively on linux-kernel -- in fact, several of the smaller architectures added support at that time. That the breakage occurred because of an inconsequential test rather than real code is thus really nothing but fortunate.

I don't disagree with any of that (except that an m68k allmodconfig build may well build fine without the test being built so maybe building the test needs a dependency).

My point was that I keep hearing the "Linus said it is OK to break the other architectures" argument brought up where it really is not even relevant to the conversation. If anything, I guess Geert was taking a little dig at me because his problem should have been noticed among my build results. Unfortunately even I don't check all the build results all the time (I guess I hope that maintainers may have time to check them out once in a while).

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/
#6 lore raw
In reply to H. Peter Anvin
Quoted 32 lines · H. Peter Anvin
Quoted 25 lines · Geert Uytterhoeven
Quoted 7 lines · Ingo Molnar

Please pull the latest x86-atomic-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip.git x86-atomic-for-linus

out-of-topic modifications in x86-atomic-for-linus:
---------------------------------------------------

Quoted 8 lines
lib/Makefile                       # 86a8938: lib: Add self-test for atomic64_t
lib/atomic64.c                     # 9757789: lib: Fix atomic64_add_unless retu
lib/atomic64_test.c                # a5c9161: x86, atomic64: In selftest, disti
                                  # 25a304f: lib: Fix atomic64_inc_not_zero te
                                  # 9efbcd5: lib: Fix atomic64_add_unless test
                                  # d7f6de1: x86: Implement atomic[64]_dec_if_
                                  # 8f4f202: lib: Only test atomic64_dec_if_po
                                  # 86a8938: lib: Add self-test for atomic64_t

Is having atomic64_t mandatory now?

According to the allmodconfig build logs
(http://kisskb.ellerman.id.au/kisskb/matrix/),
several architectures (at least m68k and mips) don't have it. Furthermore, the test fails to compile on a few architectures that do have it (parisc, s390, sh, ...).

Quoted 5 lines

<boilerplate>
It's a pity this wasn't raised/resolved between its detection in linux-next and before it entered mainline...
</boilerplate>

Quoted 5 lines

Is having atomic64_t mandatory? Not yet, I don't think, but it probably will be soon -- which is why there is a generic implementation available. All those architectures just need to select CONFIG_GENERIC_ATOMIC64 and voil??, problem solved.

No, the problem isn't solved, you apparently overlooked the part of Geert's mail that point out that the test fails to build on architecures that _do_ have atomic64_t. All of arm/parisc/powerpc/sh select GENERIC_ATOMIC64, suggesting that the test itself was only ever tested on x86 and never on the generic implementation. While that may be par for the course these days, it's still pretty poor form.

Quoted 5 lines

As far as your boilerplate is concerned, I think Linus made it clear at the Kernel Summit that is it not the obligation of x86/ARM/PowerPC to slow down to not break the smaller architectures; it's the responsibility of those architecture maintainers to keep up. Sorry.

This keeping up thing gets thrown around a lot without any real basis. Most of the GENERIC_ATOMIC64 architectures enabled support for it within days of it being merged, if they've all been broken now because of an x86-centric change then it's really not so much of a keeping up issue as it is an issue of unreviewed and untested crap being merged before anyone gets a chance to look at it.

The breakage in question isn't even related to the atomic64_t implementation, it's just some trivial header stuff. It would be nice if people would spend even a cursory amount of time looking in to these things before throwing around tired generalizations of how everyone else needs to "keep up".

#7 lore raw
Quoted 8 lines · Paul Mundt

No, the problem isn't solved, you apparently overlooked the part of Geert's mail that point out that the test fails to build on architecures that _do_ have atomic64_t. All of arm/parisc/powerpc/sh select GENERIC_ATOMIC64, suggesting that the test itself was only ever tested on x86 and never on the generic implementation. While that may be par for the course these days, it's still pretty poor form.

I don't see any atomic64_t issues in the ARM, PowerPC or MIPS builds -- which doesn't mean they're not there, but if so they're obscured by other errors. m68k is missing #include <asm-generic/atomic64.h> in <asm/atomic.h>, and SPARC64 and S390 seems to have issues due to missing <linux/kernel.h> in atomic64_test.c; the latter is indeed a bug in the test and yes, should be fixed.

-hpa