Skip to contentSkip to conversation

arm: asm/cmpxchg.h: Add support half-word xchg()

RFC PATCH In review Pranith Kumar 3 participants 10 messages Feb 25, 2015 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 arm: asm/cmpxchg.h: Add support half-word xchg()

This patch adds support for a half-word xchg() for ARM using ldrexh/strexh instructions. It also fixes an asm comment for __cmpxchg2.

Currently using a half-word xchg() results in the following splat on an ARMv7 machine.

[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at /dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!
---
 arch/arm/include/asm/cmpxchg.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
arch/arm/include/asm/cmpxchg.h +17 -1
index abb2c37..9505cca 100644
@@ -50,6 +50,16 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 			: "r" (x), "r" (ptr)
 			: "memory", "cc");
 		break;
+	case 2:
+		asm volatile("@	__xchg2\n"
+		"1:	ldrexh	%0, [%3]\n"
+		"	strexh	%1, %2, [%3]\n"
+		"	teq	%1, #0\n"
+		"	bne	1b"
+			: "=&r" (ret), "=&r" (tmp)
+			: "r" (x), "r" (ptr)
+			: "memory", "cc");
+		break;
 	case 4:
 		asm volatile("@	__xchg4\n"
 		"1:	ldrex	%0, [%3]\n"
@@ -93,6 +103,12 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 			: "memory", "cc");
 		break;
 #endif
+	case 2:
+		raw_local_irq_save(flags);
+		ret = *(volatile uint16_t *)ptr;
+		*(volatile uint16_t *)ptr = x;
+		raw_local_irq_restore(flags);
+		break;
 	default:
 		__bad_xchg(ptr, size), ret = 0;
 		break;
@@ -158,7 +174,7 @@ static inline unsigned long __cmpxchg(volatile void *ptr, unsigned long old,
 		break;
 	case 2:
 		do {
-			asm volatile("@ __cmpxchg1\n"
+			asm volatile("@ __cmpxchg2\n"
 			"	ldrexh	%1, [%2]\n"
 			"	mov	%0, #0\n"
 			"	teq	%1, %3\n"
Signature · 1 line
1.9.1
#1 lore raw
Quoted 11 lines · On Wednesday 25 February 2015 10:36:20 Pranith Kumar wrote:

This patch adds support for a half-word xchg() for ARM using ldrexh/strexh instructions. It also fixes an asm comment for __cmpxchg2.

Currently using a half-word xchg() results in the following splat on an ARMv7 machine.

[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at /dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!

Unfortunately, the BUG message seems incomplete, can you reproduce this with CONFIG_DEBUG_BUGVERBOSE enabled?

Quoted 14 lines
 arch/arm/include/asm/cmpxchg.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
arch/arm/include/asm/cmpxchg.h +3 -0
index abb2c37..9505cca 100644
@@ -50,6 +50,16 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
 			: "r" (x), "r" (ptr)
 			: "memory", "cc");
 		break;
+	case 2:
+		asm volatile("@	__xchg2\n"
+		"1:	ldrexh	%0, [%3]\n"

arch/arm/include/asm/cmpxchg.h static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size · line 65

Quoted 10 lines
arch/arm/include/asm/cmpxchg.h +7 -0
+		"	strexh	%1, %2, [%3]\n"
+		"	teq	%1, #0\n"
+		"	bne	1b"
+			: "=&r" (ret), "=&r" (tmp)
+			: "r" (x), "r" (ptr)
+			: "memory", "cc");
+		break;
 	case 4:
 		asm volatile("@	__xchg4\n"
 		"1:	ldrex	%0, [%3]\n"

Does this work on all ARMv6 or just ARMv6k?

Arnd

#2 lore raw
Quoted 7 lines · Arnd Bergmann
Quoted 6 lines · On Wednesday 25 February 2015 10:36:20 Pranith Kumar wrote:

This patch adds support for a half-word xchg() for ARM using ldrexh/strexh instructions. It also fixes an asm comment for __cmpxchg2.

Currently using a half-word xchg() results in the following splat on an ARMv7 machine.

Quoted 8 lines
Quoted 5 lines
[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at /dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!

Unfortunately, the BUG message seems incomplete, can you reproduce this with CONFIG_DEBUG_BUGVERBOSE enabled?

The bug here is in a module caused when xchg() was used on uint16_t variable. It is caused by the __bad_xchg() for 2 byte swap.

More information:

[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at
/dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!
[   45.846450] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM
[   45.852275] Modules linked in: test(O+) nvhost_vi
[   45.857012] CPU: 0 PID: 1848 Comm: insmod Tainted: G           O
3.10.24-g6a2d13a #1
[   45.864744] task: ee406580 ti: eb18c000 task.ti: eb18c000
[   45.870141] PC is at __bad_xchg+0x24/0x28
[   45.874146] LR is at __bad_xchg+0x24/0x28
Quoted 19 lines
Quoted 19 lines
 arch/arm/include/asm/cmpxchg.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
arch/arm/include/asm/cmpxchg.h +7 -0
index abb2c37..9505cca 100644
@@ -50,6 +50,16 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
                      : "r" (x), "r" (ptr)
                      : "memory", "cc");
              break;
+     case 2:
+             asm volatile("@ __xchg2\n"
+             "1:     ldrexh  %0, [%3]\n"
+             "       strexh  %1, %2, [%3]\n"
+             "       teq     %1, #0\n"
+             "       bne     1b"
+                     : "=&r" (ret), "=&r" (tmp)
Quoted 9 lines

arch/arm/include/asm/cmpxchg.h static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size · line 65

Quoted 6 lines
arch/arm/include/asm/cmpxchg.h +3 -0
+                     : "r" (x), "r" (ptr)
+                     : "memory", "cc");
+             break;
      case 4:
              asm volatile("@ __xchg4\n"
              "1:     ldrex   %0, [%3]\n"

Does this work on all ARMv6 or just ARMv6k?

ldrexh/strexh is being used in cmpxchg() in the same file in a similar manner, and the comment there says that it works for all ARCH >= ARMv6k, so not ARMv6 I guess.

Signature · 1 line
Pranith
#3 lore raw
Quoted 21 lines · On Wednesday 25 February 2015 11:11:28 Pranith Kumar wrote:
Quoted 7 lines · Arnd Bergmann
Quoted 6 lines · On Wednesday 25 February 2015 10:36:20 Pranith Kumar wrote:

This patch adds support for a half-word xchg() for ARM using ldrexh/strexh instructions. It also fixes an asm comment for __cmpxchg2.

Currently using a half-word xchg() results in the following splat on an ARMv7 machine.

Quoted 8 lines
Quoted 5 lines
[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at /dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!

Unfortunately, the BUG message seems incomplete, can you reproduce this with CONFIG_DEBUG_BUGVERBOSE enabled?

The bug here is in a module caused when xchg() was used on uint16_t variable. It is caused by the __bad_xchg() for 2 byte swap.

More information:

Quoted 11 lines
[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at
/dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!
[   45.846450] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM
[   45.852275] Modules linked in: test(O+) nvhost_vi
[   45.857012] CPU: 0 PID: 1848 Comm: insmod Tainted: G           O
3.10.24-g6a2d13a #1
[   45.864744] task: ee406580 ti: eb18c000 task.ti: eb18c000
[   45.870141] PC is at __bad_xchg+0x24/0x28
[   45.874146] LR is at __bad_xchg+0x24/0x28

I'm more interested in the backtrace here, it's possible we should fix the driver instead.

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines
 arch/arm/include/asm/cmpxchg.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
arch/arm/include/asm/cmpxchg.h +7 -0
index abb2c37..9505cca 100644
@@ -50,6 +50,16 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
                      : "r" (x), "r" (ptr)
                      : "memory", "cc");
              break;
+     case 2:
+             asm volatile("@ __xchg2\n"
+             "1:     ldrexh  %0, [%3]\n"
+             "       strexh  %1, %2, [%3]\n"
+             "       teq     %1, #0\n"
+             "       bne     1b"
+                     : "=&r" (ret), "=&r" (tmp)
Quoted 9 lines

arch/arm/include/asm/cmpxchg.h static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size · line 65

Quoted 6 lines
arch/arm/include/asm/cmpxchg.h +3 -0
+                     : "r" (x), "r" (ptr)
+                     : "memory", "cc");
+             break;
      case 4:
              asm volatile("@ __xchg4\n"
              "1:     ldrex   %0, [%3]\n"

Does this work on all ARMv6 or just ARMv6k?

Quoted 3 lines

ldrexh/strexh is being used in cmpxchg() in the same file in a similar manner, and the comment there says that it works for all ARCH >= ARMv6k, so not ARMv6 I guess.

Ok, then you need to put the same check in __xchg too. I'm not sure about the 1-byte case here, because that is already used in ARMv6 __xchg.

Arnd

#4 lore raw
Quoted 34 lines · Arnd Bergmann
Quoted 21 lines · On Wednesday 25 February 2015 11:11:28 Pranith Kumar wrote:
Quoted 7 lines · Arnd Bergmann
Quoted 6 lines · On Wednesday 25 February 2015 10:36:20 Pranith Kumar wrote:

This patch adds support for a half-word xchg() for ARM using ldrexh/strexh instructions. It also fixes an asm comment for __cmpxchg2.

Currently using a half-word xchg() results in the following splat on an ARMv7 machine.

Quoted 8 lines
Quoted 5 lines
[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at /dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!

Unfortunately, the BUG message seems incomplete, can you reproduce this with CONFIG_DEBUG_BUGVERBOSE enabled?

The bug here is in a module caused when xchg() was used on uint16_t variable. It is caused by the __bad_xchg() for 2 byte swap.

More information:

Quoted 11 lines
[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at
/dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!
[   45.846450] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM
[   45.852275] Modules linked in: test(O+) nvhost_vi
[   45.857012] CPU: 0 PID: 1848 Comm: insmod Tainted: G           O
3.10.24-g6a2d13a #1
[   45.864744] task: ee406580 ti: eb18c000 task.ti: eb18c000
[   45.870141] PC is at __bad_xchg+0x24/0x28
[   45.874146] LR is at __bad_xchg+0x24/0x28
Quoted 2 lines

I'm more interested in the backtrace here, it's possible we should fix the driver instead.

Actually, I think we ought to get rid of __bad_xchg() so that cases like this cause a link error instead of a runtime error, just like we do in other cases as well.

That's something that goes back ages (it used to be called something like invalidptr in 2.0 kernels...)

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 10.5Mbps down 400kbps up
according to speedtest.net.
#5 lore raw
Quoted 6 lines · Russell King - ARM Linux

Actually, I think we ought to get rid of __bad_xchg() so that cases like this cause a link error instead of a runtime error, just like we do in other cases as well.

That's something that goes back ages (it used to be called something like invalidptr in 2.0 kernels...)

That sounds like a good idea. I will cook up a patch doing this.

Thanks!

Signature · 1 line
Pranith
#6 lore raw
Quoted 10 lines · Pranith Kumar
Quoted 6 lines · Russell King - ARM Linux

Actually, I think we ought to get rid of __bad_xchg() so that cases like this cause a link error instead of a runtime error, just like we do in other cases as well.

That's something that goes back ages (it used to be called something like invalidptr in 2.0 kernels...)

That sounds like a good idea. I will cook up a patch doing this.

I've just removed it on my build system's tree, and we'll see tomorrow morning how it fairs. If it spits out a lot of errors, we won't be able to remove it.

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 10.5Mbps down 400kbps up
according to speedtest.net.
#7 lore raw
In reply to Arnd Bergmann
Quoted 14 lines · Arnd Bergmann
Quoted 13 lines

More information:

[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at
/dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!
[   45.846450] Internal error: Oops - BUG: 0 [#1] PREEMPT SMP ARM
[   45.852275] Modules linked in: test(O+) nvhost_vi
[   45.857012] CPU: 0 PID: 1848 Comm: insmod Tainted: G           O
3.10.24-g6a2d13a #1
[   45.864744] task: ee406580 ti: eb18c000 task.ti: eb18c000
[   45.870141] PC is at __bad_xchg+0x24/0x28
[   45.874146] LR is at __bad_xchg+0x24/0x28
Quoted 2 lines

I'm more interested in the backtrace here, it's possible we should fix the driver instead.

I should have been more clearer. I apologize. This is in a test module I wrote trying out the various synchronization primitives. This is not a problem in any current driver I am using.

Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 15 lines
 arch/arm/include/asm/cmpxchg.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
arch/arm/include/asm/cmpxchg.h +3 -0
index abb2c37..9505cca 100644
@@ -50,6 +50,16 @@ static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size
                      : "r" (x), "r" (ptr)
                      : "memory", "cc");
              break;
+     case 2:
+             asm volatile("@ __xchg2\n"
+             "1:     ldrexh  %0, [%3]\n"

arch/arm/include/asm/cmpxchg.h static inline unsigned long __xchg(unsigned long x, volatile void *ptr, int size · line 65

Quoted 10 lines
arch/arm/include/asm/cmpxchg.h +7 -0
+             "       strexh  %1, %2, [%3]\n"
+             "       teq     %1, #0\n"
+             "       bne     1b"
+                     : "=&r" (ret), "=&r" (tmp)
+                     : "r" (x), "r" (ptr)
+                     : "memory", "cc");
+             break;
      case 4:
              asm volatile("@ __xchg4\n"
              "1:     ldrex   %0, [%3]\n"

Does this work on all ARMv6 or just ARMv6k?

Quoted 8 lines
Quoted 3 lines

ldrexh/strexh is being used in cmpxchg() in the same file in a similar manner, and the comment there says that it works for all ARCH >= ARMv6k, so not ARMv6 I guess.

Ok, then you need to put the same check in __xchg too. I'm not sure about the 1-byte case here, because that is already used in ARMv6 __xchg.

OK, I will update the patch with the check.

Looking closely I see what you are saying. In __xchg(), for the 1 byte case we are using ldrexb/strexb while the test checks for version >=
6. Documentation online says that ldrex{h/b} are supported only for ARMv6k or greater.

So I think the check in __xchg() should be changed to one similar in cmpxchg(). Is that what you meant?

If so, i will send in an updated patch.

Thanks!

Signature · 1 line
Pranith
#8 lore raw
In reply to Arnd Bergmann
Quoted 7 lines · Arnd Bergmann
Quoted 6 lines · On Wednesday 25 February 2015 10:36:20 Pranith Kumar wrote:

This patch adds support for a half-word xchg() for ARM using ldrexh/strexh instructions. It also fixes an asm comment for __cmpxchg2.

Currently using a half-word xchg() results in the following splat on an ARMv7 machine.

Quoted 8 lines
Quoted 5 lines
[   45.833303] xchg: bad data size: pc 0xbe806020, ptr 0xeb18deee, size 2
[   45.833324] ------------[ cut here ]------------
[   45.837939] kernel BUG at /dvs/git/dirty/git-master_linux/kernel/arch/arm/kernel/traps.c:727!

Unfortunately, the BUG message seems incomplete, can you reproduce this with CONFIG_DEBUG_BUGVERBOSE enabled?

That isn't because CONFIG_DEBUG_BUGVERBOSE isn't set. It's because the message author decided that the remainder of the kernel bug message wasn't useful to report. After all, it's just a load of hex numbers and symbolic information. Who would want that junk.

(Some people really don't get this: we print stuff from the kernel because it gives _us_ useful information to debug problems like this, but because they don't understand it themselves, they decide they can omit it from their bug reports...)

Signature · 2 lines
FTTC broadband for 0.8mile line: currently at 10.5Mbps down 400kbps up
according to speedtest.net.
#9 lore raw
Quoted 8 lines · Russell King - ARM Linux
Quoted 2 lines

Unfortunately, the BUG message seems incomplete, can you reproduce this with CONFIG_DEBUG_BUGVERBOSE enabled?

That isn't because CONFIG_DEBUG_BUGVERBOSE isn't set. It's because the message author decided that the remainder of the kernel bug message wasn't useful to report. After all, it's just a load of hex numbers and symbolic information. Who would want that junk.

Quoted 5 lines

(Some people really don't get this: we print stuff from the kernel because it gives _us_ useful information to debug problems like this, but because they don't understand it themselves, they decide they can omit it from their bug reports...)

I apologize for not pasting the entire back trace. It was in a test module I wrote testing synchronization primitives and did not think the back trace would be useful to report.

I will include verbose details from now on.

Thanks!

Signature · 1 line
Pranith