Skip to contentSkip to conversation

ARM: Add SWP/SWPB emulation for ARMv7 processors

PATCH New Leif Lindholm 4 participants 11 messages Dec 17, 2009 – Jan 8, 2010 Arm (32-bit)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

The SWP instruction was deprecated in the ARMv6 architecture, superseded by the LDREX/STREX family of instructions for load-linked/store-conditional operations. The ARMv7 multiprocessing extensions mandate that SWP/SWPB instructions are treated as undefined from reset, with the ability to enable them through the System Control Register SW bit.

This patch adds the alternative solution to emulate the SWP and SWPB instructions using LDREX/STREX sequences, and log statistics to /proc/cpu/swp_emulation. To correctly deal with copy-on-write, it also modifies cpu_v7_set_pte_ext to change the mappings to priviliged RO when user RO.

Cc Catalin Marinas <[email protected]>
---
 arch/arm/kernel/Makefile      |    1 
 arch/arm/kernel/swp_emulate.c |  281 +++++++++++++++++++++++++++++++++++++++++
 arch/arm/mm/Kconfig           |   13 ++
 arch/arm/mm/proc-v7.S         |    6 +
 4 files changed, 301 insertions(+), 0 deletions(-)
 create mode 100644 arch/arm/kernel/swp_emulate.c
arch/arm/kernel/Makefile +1 -0
index dd00f74..d1befbc 100644
@@ -38,6 +38,7 @@ obj-$(CONFIG_ARM_THUMBEE)	+= thumbee.o
 obj-$(CONFIG_KGDB)		+= kgdb.o
 obj-$(CONFIG_ARM_UNWIND)	+= unwind.o
 obj-$(CONFIG_HAVE_TCM)		+= tcm.o
+obj-$(CONFIG_SWP_EMULATE)	+= swp_emulate.o
 
 obj-$(CONFIG_CRUNCH)		+= crunch.o crunch-bits.o
 AFLAGS_crunch-bits.o		:= -Wa,-mcpu=ep9312
arch/arm/kernel/swp_emulate.c new file +281 -0

281 lines not on this page · raw message

arch/arm/mm/Kconfig +13 -0
index dd4698c..073bfed 100644
@@ -630,6 +630,19 @@ config ARM_THUMBEE
 	  Say Y here if you have a CPU with the ThumbEE extension and code to
 	  make use of it. Say N for code that can run on CPUs without ThumbEE.
 
+config SWP_EMULATE
+	bool "Emulate SWP/SWPB instructions"
+	depends on CPU_V7
+	default y if SMP
+	help
+	  ARMv6 architecture deprecates use of the SWP/SWPB instructions.
+	  ARMv7 multiprocessing extensions introduce the ability to disable
+	  these instructions, triggering an undefined instruction exception
+	  when executed. Say Y here to enable software emulation of these
+	  instructions using LDREX/STREX.
+	  Also creates /proc/cpu/swp_emulation for statistics.
+	  If unsure, say Y.
+
 config CPU_BIG_ENDIAN
 	bool "Build big-endian kernel"
 	depends on ARCH_SUPPORTS_BIG_ENDIAN
arch/arm/mm/proc-v7.S +6 -0
index 3a28521..cb85aeb 100644
@@ -147,8 +147,10 @@ ENTRY(cpu_v7_set_pte_ext)
 
 	tst	r1, #L_PTE_USER
 	orrne	r3, r3, #PTE_EXT_AP1
+#ifndef CONFIG_SWP_EMULATE
 	tstne	r3, #PTE_EXT_APX
 	bicne	r3, r3, #PTE_EXT_APX | PTE_EXT_AP0
+#endif
 
 	tst	r1, #L_PTE_EXEC
 	orreq	r3, r3, #PTE_EXT_XN
@@ -275,6 +277,10 @@ __v7_setup:
 #ifdef CONFIG_CPU_ENDIAN_BE8
 	orr	r6, r6, #1 << 25		@ big-endian page tables
 #endif
+#ifdef CONFIG_SWP_EMULATE
+	orr     r5, r5, #(1 << 10)              @ set SW bit in "clear"
+	bic     r6, r6, #(1 << 10)              @ clear it in "mmuset"
+#endif
    	mrc	p15, 0, r0, c1, c0, 0		@ read control register
 	bic	r0, r0, r5			@ clear bits them
 	orr	r0, r0, r6			@ set them
#1 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

Quoted 12 lines

From: linux-arm-kernel-bounces at lists.infradead.org [mailto:linux-arm-kernel- bounces at lists.infradead.org] On Behalf Of Leif Lindholm
Sent: Thursday, December 17, 2009 11:54 AM

The SWP instruction was deprecated in the ARMv6 architecture, superseded by the LDREX/STREX family of instructions for load-linked/store-conditional operations. The ARMv7 multiprocessing extensions mandate that SWP/SWPB instructions are treated as undefined from reset, with the ability to enable them through the System Control Register SW bit.

Quoted 5 lines

This patch adds the alternative solution to emulate the SWP and SWPB instructions using LDREX/STREX sequences, and log statistics to /proc/cpu/swp_emulation. To correctly deal with copy-on-write, it also modifies cpu_v7_set_pte_ext to change the mappings to priviliged RO when user RO.

Exclusive operations are more certainly more efficient than the full bus lock behavior of SWP. Finding and killing SWP where possible seems positive.

Still, the exclusive operations for some systems don't extend beyond an ARM smp-cluster. Not all cores and interconnect implementations support the range lock. As such from a SOC point of view SWP still has value as it may exist where lwrex/swrex would fail. It is also much lighter than using some kind of mailbox alternative.

While the ARM is important to many SOCs its not the whole thing. I've seen some side threads where people are busy blindly removing SWP and quoting half information.

My main comment on patch in this regard is to make the point you won't be able to fully emulate a SWP with the sequence you posted if you consider other cores outside the ARM cluster. Some note to that effect would be positive in the description.

Regards,
Richard W.

#2 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

Quoted 3 lines · Richard

Exclusive operations are more certainly more efficient than the full bus lock behavior of SWP. Finding and killing SWP where possible seems positive.

Are they really more efficient for cached accesses in L1? In principle, external bus lock is not needed when doing an read-modify-write on words in MESI cache, just the ability to internally block it's eviction during the sequence.

-- Jamie

#3 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

Quoted 5 lines

From: Jamie Lokier [mailto:jamie at shareable.org]
Sent: Thursday, December 17, 2009 1:16 PM

Woodruff, Richard wrote:

Quoted 8 lines
Quoted 3 lines

Exclusive operations are more certainly more efficient than the full bus lock behavior of SWP. Finding and killing SWP where possible seems positive.

Are they really more efficient for cached accesses in L1? In principle, external bus lock is not needed when doing an read-modify-write on words in MESI cache, just the ability to internally block it's eviction during the sequence.

Performance point was mainly about coordination with non-coherent external masters (outside of cluster). Blocking all external people for a small range is not optimal.

Within the cluster and in cache as you point out... I'm not sure of implementation reality. The outside world past A9 may or may not export some form of mesi.

Regards,
Richard W.

#4 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

Quoted 9 lines · Richard
Quoted 9 lines

From: Jamie Lokier [mailto:jamie at shareable.org]
Sent: Thursday, December 17, 2009 1:16 PM

Quoted 3 lines · Richard

Exclusive operations are more certainly more efficient than the full bus lock behavior of SWP. Finding and killing SWP where possible seems positive.

Quoted 8 lines
Quoted 4 lines

Are they really more efficient for cached accesses in L1? In principle, external bus lock is not needed when doing an read-modify-write on words in MESI cache, just the ability to internally block it's eviction during the sequence.

Performance point was mainly about coordination with non-coherent external masters (outside of cluster). Blocking all external people for a small range is not optimal.

Sure, but doesn't your patch break any program which tries to do that, due to the lack of LDREX/STREX global monitor reaching to external masters, so that case isn't relevant?

So (correct me if I'm wrong) the interesting case is only about accesses within the cluster. Is that really slower with SWP/SWPB?

-- Jamie

#5 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

Quoted 5 lines

From: Jamie Lokier [mailto:jamie at shareable.org]
Sent: Friday, December 18, 2009 2:32 PM
To: Woodruff, Richard

Quoted 7 lines
Quoted 3 lines

Performance point was mainly about coordination with non-coherent external masters (outside of cluster). Blocking all external people for a small range is not optimal.

Sure, but doesn't your patch break any program which tries to do that, due to the lack of LDREX/STREX global monitor reaching to external masters, so that case isn't relevant?

I didn't submit patch, just commented on how it might change out of cluster behavior and suggested a comment added to documentation.

A swp out of cluster is atomic as the bus makes it so. A bus-bridge translated-neutered lwrex/strex will not necessary be atomic.

Quoted 2 lines

So (correct me if I'm wrong) the interesting case is only about accesses within the cluster. Is that really slower with SWP/SWPB?

I think I've lost context over holidays to answer. I have not looked at implementation details in cluster to know which sequence is faster. Inside the cluster many things which are normally slower as perceived by the ARM core can be optimized away in lower hardware layers (misaligned access as an example).

Regards,
Richard W.

#6 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

In reply to Woodruff, Richard

Thanks for the feedback.

Quoted 9 lines

From: Woodruff, Richard [mailto:r-woodruff2 at ti.com]
Sent: 17 December 2009 18:20

Still, the exclusive operations for some systems don't extend beyond an ARM smp-cluster. Not all cores and interconnect implementations support the range lock. As such from a SOC point of view SWP still has value as it may exist where lwrex/swrex would fail. It is also much lighter than using some kind of mailbox alternative.

Quoted 3 lines

While the ARM is important to many SOCs its not the whole thing. I've seen some side threads where people are busy blindly removing SWP and quoting half information.

There is nothing half about SWP/SWPB being deprecated from ARMv6 onwards. Also, not all systems properly implement the bus locking support required for SWP to be properly atomic externally.

Quoted 4 lines

My main comment on patch in this regard is to make the point you won't be able to fully emulate a SWP with the sequence you posted if you consider other cores outside the ARM cluster. Some note to that effect would be positive in the description.

I would argue that this is not a property of the patch, but rather the architectural deprecation of the instruction(s).

However, I take your point. What if I add a comment that this emulation will not work on uncached memory regions where no global exclusive monitor is implemented?

Regards

	/
		Leif
#7 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

In reply to Leif Lindholm
Quoted 2 lines · Leif Lindholm

To correctly deal with copy-on-write, it also modifies cpu_v7_set_pte_ext to change the mappings to priviliged RO when user RO.

Does this break ptrace writing to RO pages or anything else? If it doesn't break anything, seems to me it should always be done like that, not conditional on CONFIG_SWP_EMULATE.

arch/arm/kernel/swp_emulate.c line 126

Quoted 8 lines
arch/arm/kernel/swp_emulate.c new file +8 -0
+static void set_segfault(struct pt_regs *regs)
+{
+	siginfo_t info;
+
+	info.si_signo = SIGSEGV;
+	info.si_errno = 0;
+	info.si_code  = SEGV_ACCERR;
+	info.si_addr  = (void __user *)regs->ARM_pc;

Ideally it would report the same errors as userspace accesses, as calculated in do_page_fault() and passed to __do_user_fault(), for the benefit of programs which distinguish SEGV_MAPERR from SEGV_ACCERR.

arch/arm/kernel/swp_emulate.c line 143

Quoted 8 lines
arch/arm/kernel/swp_emulate.c new file +7 -0
+	printk(KERN_INFO "SWP{B} emulation: access caused memory abort!\n");
+	arm_notify_die("Illegal memory access", regs, &info, 0, 0);
 
+	if (address & 0x3) {
+		/* SWP to unaligned address not permitted */
+		printk(KERN_INFO "SWP instruction on unaligned pointer!\n");
+		return -EFAULT;
+	}

These printks look like an easy local denial of service. They should be rate limited.

-- Jamie

#8 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

Quoted 4 lines · Jamie Lokier
Quoted 2 lines · Leif Lindholm
arch/arm/kernel/swp_emulate.c new file +2 -0
+     printk(KERN_INFO "SWP{B} emulation: access caused memory abort!\n");
+     arm_notify_die("Illegal memory access", regs, &info, 0, 0);
Quoted 8 lines

arch/arm/kernel/swp_emulate.c line 143

Quoted 5 lines
arch/arm/kernel/swp_emulate.c new file +5 -0
+     if (address & 0x3) {
+             /* SWP to unaligned address not permitted */
+             printk(KERN_INFO "SWP instruction on unaligned pointer!\n");
+             return -EFAULT;
+     }

These printks look like an easy local denial of service. They should be rate limited.

pr_debug() may be a better choice.

Signature · 1 line
Catalin
#9 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

In reply to Jamie Lokier
Quoted 7 lines · Jamie Lokier
Quoted 2 lines · Leif Lindholm

To correctly deal with copy-on-write, it also modifies cpu_v7_set_pte_ext to change the mappings to priviliged RO when user RO.

Does this break ptrace writing to RO pages or anything else? If it doesn't break anything, seems to me it should always be done like that, not conditional on CONFIG_SWP_EMULATE.

ptrace does COW otherwise it would affect other tasks sharing the RO pages. The only thing user RO, kernel RW is needed in the kernel is for the vectors page when a TLS register is not available. This is not the case for ARMv7, so we can make it unconditional.

BTW, the patches I posted for the revised page table format with SCTLR.AFE do this already (but I'm not sure when/if they'll go in).

Signature · 1 line
Catalin
#10 lore raw

PATCH 1/1 ARM: Add SWP/SWPB emulation for ARMv7 processors

In reply to Jamie Lokier
Quoted 9 lines

-----Original Message-----
From: Jamie Lokier [mailto:jamie at shareable.org]
Sent: 17 December 2009 19:32

Quoted 3 lines

To correctly deal with copy-on-write, it also modifies cpu_v7_set_pte_ext to change the mappings to priviliged RO when user RO.

Does this break ptrace writing to RO pages or anything else?

I believe Catalin has answered this question.

Quoted 2 lines

If it doesn't break anything, seems to me it should always be done like that, not conditional on CONFIG_SWP_EMULATE.

I agree this should not really be conditional on CONFIG_SWP_EMULATE, but since Catalin's revised page table format patches have not been merged, it felt less contentious to leave that behaviour in place unless real need to change it existed. I'd be happy to drop the ifndef if that was preferred.

Quoted 3 lines

Ideally it would report the same errors as userspace accesses, as calculated in do_page_fault() and passed to __do_user_fault(), for the benefit of programs which distinguish SEGV_MAPERR from SEGV_ACCERR.

Good point - I'm adding this and will be submitting an updated patch shortly.

Quoted 2 lines

These printks look like an easy local denial of service. They should be rate limited.

Ok, I'll replace them with pr_debug().

/
Leif