2013-09-06 21:30:13

by Behan Webster

[permalink] [raw]
Subject: [PATCH 0/5] arm: LLVMLinux: Add current_stack_pointer

From: Behan Webster <[email protected]>

The LLVMLinux Project is working to be able to build the Linux kernel with
clang/LLVM. With the release of LLVM 3.3 clang is now able to compile the Linux
kernel with a number of small patches (available from the LLVMLinux git repo).

These patches add a macro to get the current stack pointer which allows for a
single place in which to do so with ASM. Before this named registers (a gcc
extension) was used to get the stack pointer. Using ASM is a more portable way
of getting the stack pointer which works with both gcc and clang. This macro
is of the same name used in the X86 arch.

Behan Webster (5):
arm: LLVMLinux: Add current_stack_pointer macro for ARM
arm: LLVMLinux: use current_stack_pointer for percpu
arm: LLVMLinux: Use current_stack_pointer for return_address
arm: LLVMLinux: Use current_stack_pointer in save_stack_trace_tsk
arm: LLVMLinux: Use current_stack_pointer in unwind_backtrace

arch/arm/include/asm/percpu.h | 4 ++--
arch/arm/include/asm/thread_info.h | 9 +++++++++
arch/arm/kernel/return_address.c | 3 +--
arch/arm/kernel/stacktrace.c | 4 +---
arch/arm/kernel/unwind.c | 3 +--
5 files changed, 14 insertions(+), 9 deletions(-)

--
1.8.1.2


2013-09-06 21:30:40

by Behan Webster

[permalink] [raw]
Subject: [PATCH 1/5] arm: LLVMLinux: Add current_stack_pointer macro for ARM

From: Behan Webster <[email protected]>

A macro to get the current stack pointer which allows for a single place in
which to do so with ASM. Before this named registers (a gcc extension) was used
to get the stack pointer. Using ASM is a more portable way of getting the stack
pointer which works with both gcc and clang. This macro is of the same name
used in the X86 arch.

Author: Behan Webster <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Reviewed-by: Jan-Simon Möller <[email protected]>
---
arch/arm/include/asm/thread_info.h | 9 +++++++++
1 file changed, 9 insertions(+)

diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
index df5e13d..94283f8 100644
--- a/arch/arm/include/asm/thread_info.h
+++ b/arch/arm/include/asm/thread_info.h
@@ -100,6 +100,15 @@ struct thread_info {
#define init_stack (init_thread_union.stack)

/*
+ * how to get the current stack pointer from C
+ */
+#define current_stack_pointer ({ \
+ unsigned long current_sp; \
+ asm ("mov %0, r13" : "=r" (current_sp)); \
+ current_sp; \
+})
+
+/*
* how to get the thread information struct from C
*/
static inline struct thread_info *current_thread_info(void) __attribute_const__;
--
1.8.1.2

2013-09-06 21:30:54

by Behan Webster

[permalink] [raw]
Subject: [PATCH 3/5] arm: LLVMLinux: Use current_stack_pointer for return_address

From: Behan Webster <[email protected]>

The existing code uses named registers to get the value of the stack pointer.
The new current_stack_pointer macro is more readable and allows for a central
portable implementation of how to get the stack pointer with ASM. This change
supports being able to compile the kernel with both gcc and Clang.

Signed-off-by: Mark Charlebois <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Reviewed-by: Jan-Simon Möller <[email protected]>
---
arch/arm/kernel/return_address.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/arm/kernel/return_address.c b/arch/arm/kernel/return_address.c
index fafedd8..5bceaef 100644
--- a/arch/arm/kernel/return_address.c
+++ b/arch/arm/kernel/return_address.c
@@ -39,13 +39,12 @@ void *return_address(unsigned int level)
{
struct return_address_data data;
struct stackframe frame;
- register unsigned long current_sp asm ("sp");

data.level = level + 2;
data.addr = NULL;

frame.fp = (unsigned long)__builtin_frame_address(0);
- frame.sp = current_sp;
+ frame.sp = current_stack_pointer;
frame.lr = (unsigned long)__builtin_return_address(0);
frame.pc = (unsigned long)return_address;

--
1.8.1.2

2013-09-06 21:30:44

by Behan Webster

[permalink] [raw]
Subject: [PATCH 2/5] arm: LLVMLinux: use current_stack_pointer for percpu

From: Behan Webster <[email protected]>

The existing code uses named registers to get the value of the stack pointer.
The new current_stack_pointer macro is more readable and allows for a central
portable implementation of how to get the stack pointer with ASM. This change
supports being able to compile the kernel with both gcc and Clang.

Signed-off-by: Mark Charlebois <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Reviewed-by: Jan-Simon Möller <[email protected]>
---
arch/arm/include/asm/percpu.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/arch/arm/include/asm/percpu.h b/arch/arm/include/asm/percpu.h
index 209e650..629a975 100644
--- a/arch/arm/include/asm/percpu.h
+++ b/arch/arm/include/asm/percpu.h
@@ -30,14 +30,14 @@ static inline void set_my_cpu_offset(unsigned long off)
static inline unsigned long __my_cpu_offset(void)
{
unsigned long off;
- register unsigned long *sp asm ("sp");
+ unsigned long sp = current_stack_pointer;

/*
* Read TPIDRPRW.
* We want to allow caching the value, so avoid using volatile and
* instead use a fake stack read to hazard against barrier().
*/
- asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (*sp));
+ asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (sp));

return off;
}
--
1.8.1.2

2013-09-06 21:31:11

by Behan Webster

[permalink] [raw]
Subject: [PATCH 4/5] arm: LLVMLinux: Use current_stack_pointer in save_stack_trace_tsk

From: Behan Webster <[email protected]>

The existing code uses named registers to get the value of the stack pointer.
The new current_stack_pointer macro is more readable and allows for a central
portable implementation of how to get the stack pointer with ASM. This change
supports being able to compile the kernel with both gcc and Clang.

Signed-off-by: Mark Charlebois <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Reviewed-by: Jan-Simon Möller <[email protected]>
---
arch/arm/kernel/stacktrace.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/arch/arm/kernel/stacktrace.c b/arch/arm/kernel/stacktrace.c
index 00f79e5..8c23310 100644
--- a/arch/arm/kernel/stacktrace.c
+++ b/arch/arm/kernel/stacktrace.c
@@ -109,11 +109,9 @@ void save_stack_trace_tsk(struct task_struct *tsk, struct stack_trace *trace)
frame.pc = thread_saved_pc(tsk);
#endif
} else {
- register unsigned long current_sp asm ("sp");
-
data.no_sched_functions = 0;
frame.fp = (unsigned long)__builtin_frame_address(0);
- frame.sp = current_sp;
+ frame.sp = current_stack_pointer;
frame.lr = (unsigned long)__builtin_return_address(0);
frame.pc = (unsigned long)save_stack_trace_tsk;
}
--
1.8.1.2

2013-09-06 21:31:18

by Behan Webster

[permalink] [raw]
Subject: [PATCH 5/5] arm: LLVMLinux: Use current_stack_pointer in unwind_backtrace

From: Behan Webster <[email protected]>

The existing code uses named registers to get the value of the stack pointer.
The new current_stack_pointer macro is more readable and allows for a central
portable implementation of how to get the stack pointer with ASM. This change
supports being able to compile the kernel with both gcc and Clang.

Signed-off-by: Mark Charlebois <[email protected]>
Signed-off-by: Behan Webster <[email protected]>
Reviewed-by: Jan-Simon Möller <[email protected]>
---
arch/arm/kernel/unwind.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/arch/arm/kernel/unwind.c b/arch/arm/kernel/unwind.c
index 00df012..e7f1eec 100644
--- a/arch/arm/kernel/unwind.c
+++ b/arch/arm/kernel/unwind.c
@@ -408,7 +408,6 @@ int unwind_frame(struct stackframe *frame)
void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
{
struct stackframe frame;
- register unsigned long current_sp asm ("sp");

pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk);

@@ -424,7 +423,7 @@ void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk)
? regs->ARM_pc : regs->ARM_lr;
} else if (tsk == current) {
frame.fp = (unsigned long)__builtin_frame_address(0);
- frame.sp = current_sp;
+ frame.sp = current_stack_pointer;
frame.lr = (unsigned long)__builtin_return_address(0);
frame.pc = (unsigned long)unwind_backtrace;
} else {
--
1.8.1.2

2013-09-06 22:19:59

by Måns Rullgård

[permalink] [raw]
Subject: Re: [PATCH 1/5] arm: LLVMLinux: Add current_stack_pointer macro for ARM

[email protected] writes:

> +#define current_stack_pointer ({ \
> + unsigned long current_sp; \
> + asm ("mov %0, r13" : "=r" (current_sp)); \
> + current_sp; \
> +})

Why do you use 'r13' rather than the more common 'sp' alias?

--
M?ns Rullg?rd
[email protected]

2013-09-06 22:26:15

by Russell King - ARM Linux

[permalink] [raw]
Subject: Re: [PATCH 1/5] arm: LLVMLinux: Add current_stack_pointer macro for ARM

On Fri, Sep 06, 2013 at 05:28:07PM -0400, [email protected] wrote:
> From: Behan Webster <[email protected]>
>
> A macro to get the current stack pointer which allows for a single place in
> which to do so with ASM. Before this named registers (a gcc extension) was used
> to get the stack pointer. Using ASM is a more portable way of getting the stack
> pointer which works with both gcc and clang. This macro is of the same name
> used in the X86 arch.

This will result in less optimal code - rather than the compiler being
able to mask directly with 'sp', it's going to have to use this bit of
assembly to first move it into another register.

Why do we want this change?

2013-09-06 22:28:22

by Russell King - ARM Linux

[permalink] [raw]
Subject: Re: [PATCH 2/5] arm: LLVMLinux: use current_stack_pointer for percpu

On Fri, Sep 06, 2013 at 05:28:08PM -0400, [email protected] wrote:
> From: Behan Webster <[email protected]>
>
> The existing code uses named registers to get the value of the stack pointer.
> The new current_stack_pointer macro is more readable and allows for a central
> portable implementation of how to get the stack pointer with ASM. This change
> supports being able to compile the kernel with both gcc and Clang.
>
> Signed-off-by: Mark Charlebois <[email protected]>
> Signed-off-by: Behan Webster <[email protected]>
> Reviewed-by: Jan-Simon M?ller <[email protected]>
> ---
> arch/arm/include/asm/percpu.h | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm/include/asm/percpu.h b/arch/arm/include/asm/percpu.h
> index 209e650..629a975 100644
> --- a/arch/arm/include/asm/percpu.h
> +++ b/arch/arm/include/asm/percpu.h
> @@ -30,14 +30,14 @@ static inline void set_my_cpu_offset(unsigned long off)
> static inline unsigned long __my_cpu_offset(void)
> {
> unsigned long off;
> - register unsigned long *sp asm ("sp");
> + unsigned long sp = current_stack_pointer;
>
> /*
> * Read TPIDRPRW.
> * We want to allow caching the value, so avoid using volatile and
> * instead use a fake stack read to hazard against barrier().
> */
> - asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (*sp));
> + asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (sp));

This looks like it's breaking what's going on here. With the original
code, we're passing the contents of the word at the stack pointer into
the assembly via a "Q" constraint. After this change, we're passing
the _value_ of the stack pointer.

Also, if you read the comment, it's certainly wrong.

2013-09-06 22:31:29

by Måns Rullgård

[permalink] [raw]
Subject: Re: [PATCH 2/5] arm: LLVMLinux: use current_stack_pointer for percpu

[email protected] writes:

> From: Behan Webster <[email protected]>
>
> The existing code uses named registers to get the value of the stack pointer.
> The new current_stack_pointer macro is more readable and allows for a central
> portable implementation of how to get the stack pointer with ASM. This change
> supports being able to compile the kernel with both gcc and Clang.
>
> Signed-off-by: Mark Charlebois <[email protected]>
> Signed-off-by: Behan Webster <[email protected]>
> Reviewed-by: Jan-Simon M?ller <[email protected]>
> ---
> arch/arm/include/asm/percpu.h | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm/include/asm/percpu.h b/arch/arm/include/asm/percpu.h
> index 209e650..629a975 100644
> --- a/arch/arm/include/asm/percpu.h
> +++ b/arch/arm/include/asm/percpu.h
> @@ -30,14 +30,14 @@ static inline void set_my_cpu_offset(unsigned long off)
> static inline unsigned long __my_cpu_offset(void)
> {
> unsigned long off;
> - register unsigned long *sp asm ("sp");
> + unsigned long sp = current_stack_pointer;
>
> /*
> * Read TPIDRPRW.
> * We want to allow caching the value, so avoid using volatile and
> * instead use a fake stack read to hazard against barrier().
> */
> - asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (*sp));
> + asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (sp));

This doesn't do quite the same thing. The existing code pretends to
read something from the stack in order to create a barrier of some
sort. Your new code stores the value of the stack pointer to a location
on the stack for consumption by the "Q" memory constraint. This store
is not necessary and should preferably be avoided.

--
M?ns Rullg?rd
[email protected]

2013-09-07 05:12:06

by Nicolas Pitre

[permalink] [raw]
Subject: Re: [PATCH 2/5] arm: LLVMLinux: use current_stack_pointer for percpu

On Fri, 6 Sep 2013, [email protected] wrote:

> From: Behan Webster <[email protected]>
>
> The existing code uses named registers to get the value of the stack pointer.
> The new current_stack_pointer macro is more readable and allows for a central
> portable implementation of how to get the stack pointer with ASM. This change
> supports being able to compile the kernel with both gcc and Clang.
>
> Signed-off-by: Mark Charlebois <[email protected]>
> Signed-off-by: Behan Webster <[email protected]>
> Reviewed-by: Jan-Simon Möller <[email protected]>
> ---
> arch/arm/include/asm/percpu.h | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/arm/include/asm/percpu.h b/arch/arm/include/asm/percpu.h
> index 209e650..629a975 100644
> --- a/arch/arm/include/asm/percpu.h
> +++ b/arch/arm/include/asm/percpu.h
> @@ -30,14 +30,14 @@ static inline void set_my_cpu_offset(unsigned long off)
> static inline unsigned long __my_cpu_offset(void)
> {
> unsigned long off;
> - register unsigned long *sp asm ("sp");
> + unsigned long sp = current_stack_pointer;
>
> /*
> * Read TPIDRPRW.
> * We want to allow caching the value, so avoid using volatile and
> * instead use a fake stack read to hazard against barrier().
> */
> - asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (*sp));
> + asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (sp));

This change doesn't look to be equivalent. Previously the *sp implied a
memory location which doesn't appear to be the case anymore.

this sp trickery was introduced in commit 509eb76ebf97 to solve bad code
generation (the commit log has the details). It would be good if Will
Deacon could confirm that his test case still works fine with your
change.


Nicolas

2013-09-09 10:05:30

by Will Deacon

[permalink] [raw]
Subject: Re: [PATCH 2/5] arm: LLVMLinux: use current_stack_pointer for percpu

Hi guys,

On Sat, Sep 07, 2013 at 06:12:03AM +0100, Nicolas Pitre wrote:
> On Fri, 6 Sep 2013, [email protected] wrote:
> > From: Behan Webster <[email protected]>
> >
> > The existing code uses named registers to get the value of the stack pointer.
> > The new current_stack_pointer macro is more readable and allows for a central
> > portable implementation of how to get the stack pointer with ASM. This change
> > supports being able to compile the kernel with both gcc and Clang.
> >
> > Signed-off-by: Mark Charlebois <[email protected]>
> > Signed-off-by: Behan Webster <[email protected]>
> > Reviewed-by: Jan-Simon M?ller <[email protected]>
> > ---
> > arch/arm/include/asm/percpu.h | 4 ++--
> > 1 file changed, 2 insertions(+), 2 deletions(-)
> >
> > diff --git a/arch/arm/include/asm/percpu.h b/arch/arm/include/asm/percpu.h
> > index 209e650..629a975 100644
> > --- a/arch/arm/include/asm/percpu.h
> > +++ b/arch/arm/include/asm/percpu.h
> > @@ -30,14 +30,14 @@ static inline void set_my_cpu_offset(unsigned long off)
> > static inline unsigned long __my_cpu_offset(void)
> > {
> > unsigned long off;
> > - register unsigned long *sp asm ("sp");
> > + unsigned long sp = current_stack_pointer;
> >
> > /*
> > * Read TPIDRPRW.
> > * We want to allow caching the value, so avoid using volatile and
> > * instead use a fake stack read to hazard against barrier().
> > */
> > - asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (*sp));
> > + asm("mrc p15, 0, %0, c13, c0, 4" : "=r" (off) : "Q" (sp));
>
> This change doesn't look to be equivalent. Previously the *sp implied a
> memory location which doesn't appear to be the case anymore.

Having looked at the other comments in this thread, I had a crack with the
following diff:

diff --git a/arch/arm/include/asm/percpu.h b/arch/arm/include/asm/percpu.h
index 209e650..ae0ac4e 100644
--- a/arch/arm/include/asm/percpu.h
+++ b/arch/arm/include/asm/percpu.h
@@ -30,7 +30,8 @@ static inline void set_my_cpu_offset(unsigned long off)
static inline unsigned long __my_cpu_offset(void)
{
unsigned long off;
- register unsigned long *sp asm ("sp");
+// register unsigned long *sp asm ("sp");
+ unsigned long *sp = (unsigned long *)current_stack_pointer;

/*
* Read TPIDRPRW.
diff --git a/arch/arm/include/asm/thread_info.h b/arch/arm/include/asm/thread_info.h
index 2b8114f..88a587c 100644
--- a/arch/arm/include/asm/thread_info.h
+++ b/arch/arm/include/asm/thread_info.h
@@ -89,6 +89,15 @@ struct thread_info {
#define init_stack (init_thread_union.stack)

/*
+ * how to get the current stack pointer from C
+ */
+#define current_stack_pointer ({ \
+ unsigned long current_sp; \
+ asm ("mov %0, r13" : "=r" (current_sp)); \
+ current_sp; \
+})
+
+/*
* how to get the thread information struct from C
*/
static inline struct thread_info *current_thread_info(void) __attribute_const__;

> this sp trickery was introduced in commit 509eb76ebf97 to solve bad code
> generation (the commit log has the details). It would be good if Will
> Deacon could confirm that his test case still works fine with your
> change.

I resurrected your original test case for the patch in question (see below)
and the code is worse with the new sp accessor, since it insists on moving
sp into r3, which forces us to push r4 onto the stack:

Before:

c001ce6c: ee1d3f90 mrc 15, 0, r3, cr13, cr0, {4}
c001ce70: e790c103 ldr ip, [r0, r3, lsl #2]
c001ce74: ee1d3f90 mrc 15, 0, r3, cr13, cr0, {4}
c001ce78: e7911103 ldr r1, [r1, r3, lsl #2]
c001ce7c: e7d22003 ldrb r2, [r2, r3]
c001ce80: ee1d3f90 mrc 15, 0, r3, cr13, cr0, {4}
c001ce84: e7903103 ldr r3, [r0, r3, lsl #2]
c001ce88: e08c0001 add r0, ip, r1
c001ce8c: e0800002 add r0, r0, r2
c001ce90: e0800003 add r0, r0, r3
c001ce94: e12fff1e bx lr

After:

c001ce74: e52d4004 push {r4} ; (str r4, [sp, #-4]!)
c001ce78: e1a0300d mov r3, sp
c001ce7c: ee1dcf90 mrc 15, 0, ip, cr13, cr0, {4}
c001ce80: e790410c ldr r4, [r0, ip, lsl #2]
c001ce84: ee1dcf90 mrc 15, 0, ip, cr13, cr0, {4}
c001ce88: e791110c ldr r1, [r1, ip, lsl #2]
c001ce8c: e7d2200c ldrb r2, [r2, ip]
c001ce90: ee1d3f90 mrc 15, 0, r3, cr13, cr0, {4}
c001ce94: e7903103 ldr r3, [r0, r3, lsl #2]
c001ce98: e0840001 add r0, r4, r1
c001ce9c: e0800002 add r0, r0, r2
c001cea0: e0800003 add r0, r0, r3
c001cea4: e8bd0010 pop {r4}
c001cea8: e12fff1e bx lr

Will

--->8

int foo(int *a, int *b, char *c)
{
int x, y, z;

x = a[__my_cpu_offset];
barrier();
y = b[__my_cpu_offset];
z = c[__my_cpu_offset];
barrier();
return x + y + z + a[__my_cpu_offset];
}