2021-09-01 14:16:37

by Vasily Gorbik

[permalink] [raw]
Subject: [PATCH] s390/unwind: use current_frame_address() to unwind current task

current_stack_pointer() simply returns current value of %r15. If
current_stack_pointer() caller allocates stack (which is the case in
unwind code) %r15 points to a stack frame allocated for callees, meaning
current_stack_pointer() caller (e.g. stack_trace_save) will end up in
the stacktrace. This is not expected by stack_trace_save*() callers and
causes problems.

current_frame_address() on the other hand returns function stack frame
address, which matches %r15 upon function invocation. Using it in
get_stack_pointer() makes it more aligned with x86 implementation
(according to BACKTRACE_SELF_TEST output) and meets stack_trace_save*()
caller's expectations, notably KCSAN.

Also make sure unwind_start is always inlined.

Reported-by: Nathan Chancellor <[email protected]>
Suggested-by: Marco Elver <[email protected]>
Signed-off-by: Vasily Gorbik <[email protected]>
---
arch/s390/include/asm/stacktrace.h | 20 ++++++++++----------
arch/s390/include/asm/unwind.h | 8 ++++----
2 files changed, 14 insertions(+), 14 deletions(-)

diff --git a/arch/s390/include/asm/stacktrace.h b/arch/s390/include/asm/stacktrace.h
index 3d8a4b94c620..22c41d7fd95c 100644
--- a/arch/s390/include/asm/stacktrace.h
+++ b/arch/s390/include/asm/stacktrace.h
@@ -34,16 +34,6 @@ static inline bool on_stack(struct stack_info *info,
return addr >= info->begin && addr + len <= info->end;
}

-static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
- struct pt_regs *regs)
-{
- if (regs)
- return (unsigned long) kernel_stack_pointer(regs);
- if (task == current)
- return current_stack_pointer();
- return (unsigned long) task->thread.ksp;
-}
-
/*
* Stack layout of a C stack frame.
*/
@@ -74,6 +64,16 @@ struct stack_frame {
((unsigned long)__builtin_frame_address(0) - \
offsetof(struct stack_frame, back_chain))

+static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
+ struct pt_regs *regs)
+{
+ if (regs)
+ return (unsigned long) kernel_stack_pointer(regs);
+ if (task == current)
+ return current_frame_address();
+ return (unsigned long) task->thread.ksp;
+}
+
/*
* To keep this simple mark register 2-6 as being changed (volatile)
* by the called function, even though register 6 is saved/nonvolatile.
diff --git a/arch/s390/include/asm/unwind.h b/arch/s390/include/asm/unwind.h
index de9006b0cfeb..5ebf534ef753 100644
--- a/arch/s390/include/asm/unwind.h
+++ b/arch/s390/include/asm/unwind.h
@@ -55,10 +55,10 @@ static inline bool unwind_error(struct unwind_state *state)
return state->error;
}

-static inline void unwind_start(struct unwind_state *state,
- struct task_struct *task,
- struct pt_regs *regs,
- unsigned long first_frame)
+static __always_inline void unwind_start(struct unwind_state *state,
+ struct task_struct *task,
+ struct pt_regs *regs,
+ unsigned long first_frame)
{
task = task ?: current;
first_frame = first_frame ?: get_stack_pointer(task, regs);
--
2.25.4


2021-09-01 17:53:17

by Marco Elver

[permalink] [raw]
Subject: Re: [PATCH] s390/unwind: use current_frame_address() to unwind current task

On Wed, 1 Sept 2021 at 16:06, Vasily Gorbik <[email protected]> wrote:
> current_stack_pointer() simply returns current value of %r15. If
> current_stack_pointer() caller allocates stack (which is the case in
> unwind code) %r15 points to a stack frame allocated for callees, meaning
> current_stack_pointer() caller (e.g. stack_trace_save) will end up in
> the stacktrace. This is not expected by stack_trace_save*() callers and
> causes problems.
>
> current_frame_address() on the other hand returns function stack frame
> address, which matches %r15 upon function invocation. Using it in
> get_stack_pointer() makes it more aligned with x86 implementation
> (according to BACKTRACE_SELF_TEST output) and meets stack_trace_save*()
> caller's expectations, notably KCSAN.
>
> Also make sure unwind_start is always inlined.
>
> Reported-by: Nathan Chancellor <[email protected]>
> Suggested-by: Marco Elver <[email protected]>
> Signed-off-by: Vasily Gorbik <[email protected]>

Tested-by: Marco Elver <[email protected]>

Thanks!

> ---
> arch/s390/include/asm/stacktrace.h | 20 ++++++++++----------
> arch/s390/include/asm/unwind.h | 8 ++++----
> 2 files changed, 14 insertions(+), 14 deletions(-)
>
> diff --git a/arch/s390/include/asm/stacktrace.h b/arch/s390/include/asm/stacktrace.h
> index 3d8a4b94c620..22c41d7fd95c 100644
> --- a/arch/s390/include/asm/stacktrace.h
> +++ b/arch/s390/include/asm/stacktrace.h
> @@ -34,16 +34,6 @@ static inline bool on_stack(struct stack_info *info,
> return addr >= info->begin && addr + len <= info->end;
> }
>
> -static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
> - struct pt_regs *regs)
> -{
> - if (regs)
> - return (unsigned long) kernel_stack_pointer(regs);
> - if (task == current)
> - return current_stack_pointer();
> - return (unsigned long) task->thread.ksp;
> -}
> -
> /*
> * Stack layout of a C stack frame.
> */
> @@ -74,6 +64,16 @@ struct stack_frame {
> ((unsigned long)__builtin_frame_address(0) - \
> offsetof(struct stack_frame, back_chain))
>
> +static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
> + struct pt_regs *regs)
> +{
> + if (regs)
> + return (unsigned long) kernel_stack_pointer(regs);
> + if (task == current)
> + return current_frame_address();
> + return (unsigned long) task->thread.ksp;
> +}
> +
> /*
> * To keep this simple mark register 2-6 as being changed (volatile)
> * by the called function, even though register 6 is saved/nonvolatile.
> diff --git a/arch/s390/include/asm/unwind.h b/arch/s390/include/asm/unwind.h
> index de9006b0cfeb..5ebf534ef753 100644
> --- a/arch/s390/include/asm/unwind.h
> +++ b/arch/s390/include/asm/unwind.h
> @@ -55,10 +55,10 @@ static inline bool unwind_error(struct unwind_state *state)
> return state->error;
> }
>
> -static inline void unwind_start(struct unwind_state *state,
> - struct task_struct *task,
> - struct pt_regs *regs,
> - unsigned long first_frame)
> +static __always_inline void unwind_start(struct unwind_state *state,
> + struct task_struct *task,
> + struct pt_regs *regs,
> + unsigned long first_frame)
> {
> task = task ?: current;
> first_frame = first_frame ?: get_stack_pointer(task, regs);
> --
> 2.25.4

2021-09-01 22:26:41

by Heiko Carstens

[permalink] [raw]
Subject: Re: [PATCH] s390/unwind: use current_frame_address() to unwind current task

On Wed, Sep 01, 2021 at 07:51:06PM +0200, Marco Elver wrote:
> On Wed, 1 Sept 2021 at 16:06, Vasily Gorbik <[email protected]> wrote:
> > current_stack_pointer() simply returns current value of %r15. If
> > current_stack_pointer() caller allocates stack (which is the case in
> > unwind code) %r15 points to a stack frame allocated for callees, meaning
> > current_stack_pointer() caller (e.g. stack_trace_save) will end up in
> > the stacktrace. This is not expected by stack_trace_save*() callers and
> > causes problems.
> >
> > current_frame_address() on the other hand returns function stack frame
> > address, which matches %r15 upon function invocation. Using it in
> > get_stack_pointer() makes it more aligned with x86 implementation
> > (according to BACKTRACE_SELF_TEST output) and meets stack_trace_save*()
> > caller's expectations, notably KCSAN.
> >
> > Also make sure unwind_start is always inlined.
> >
> > Reported-by: Nathan Chancellor <[email protected]>
> > Suggested-by: Marco Elver <[email protected]>
> > Signed-off-by: Vasily Gorbik <[email protected]>
>
> Tested-by: Marco Elver <[email protected]>
>
> Thanks!
>
> > ---
> > arch/s390/include/asm/stacktrace.h | 20 ++++++++++----------
> > arch/s390/include/asm/unwind.h | 8 ++++----
> > 2 files changed, 14 insertions(+), 14 deletions(-)

Applied, thanks!

2021-09-03 23:43:59

by Nathan Chancellor

[permalink] [raw]
Subject: Re: [PATCH] s390/unwind: use current_frame_address() to unwind current task

On Wed, Sep 01, 2021 at 04:05:59PM +0200, Vasily Gorbik wrote:
> current_stack_pointer() simply returns current value of %r15. If
> current_stack_pointer() caller allocates stack (which is the case in
> unwind code) %r15 points to a stack frame allocated for callees, meaning
> current_stack_pointer() caller (e.g. stack_trace_save) will end up in
> the stacktrace. This is not expected by stack_trace_save*() callers and
> causes problems.
>
> current_frame_address() on the other hand returns function stack frame
> address, which matches %r15 upon function invocation. Using it in
> get_stack_pointer() makes it more aligned with x86 implementation
> (according to BACKTRACE_SELF_TEST output) and meets stack_trace_save*()
> caller's expectations, notably KCSAN.
>
> Also make sure unwind_start is always inlined.
>
> Reported-by: Nathan Chancellor <[email protected]>
> Suggested-by: Marco Elver <[email protected]>
> Signed-off-by: Vasily Gorbik <[email protected]>

Sorry for the late response and I see that this has already been applied
but I took this for a spin and all of the tests pass with clang-14 in
QEMU. Thank you for the quick fix so that we can get this turned on in
CI :)

[ 10.362073] ok 1 - test_basic
[ 13.870386] ok 2 - test_concurrent_races
[ 17.379643] ok 3 - test_novalue_change
[ 17.393315] ok 4 - test_novalue_change_exception
[ 17.409815] ok 5 - test_unknown_origin
[ 20.914289] ok 6 - test_write_write_assume_atomic
[ 20.982545] ok 7 - test_write_write_struct
[ 21.106135] ok 8 - test_write_write_struct_part
[ 24.622205] ok 9 - test_read_atomic_write_atomic
[ 24.662048] ok 10 - test_read_plain_atomic_write
[ 24.775291] ok 11 - test_read_plain_atomic_rmw
[ 28.294457] ok 12 - test_zero_size_access
[ 31.829529] ok 13 - test_data_race
[ 31.867174] ok 14 - test_assert_exclusive_writer
[ 31.929184] ok 15 - test_assert_exclusive_access
[ 35.446281] ok 16 - test_assert_exclusive_access_writer
[ 35.540228] ok 17 - test_assert_exclusive_bits_change
[ 39.052271] ok 18 - test_assert_exclusive_bits_nochange
[ 39.097020] ok 19 - test_assert_exclusive_writer_scoped
[ 39.152914] ok 20 - test_assert_exclusive_access_scoped
[ 42.675158] ok 21 - test_jiffies_noreport
[ 46.192453] ok 22 - test_seqlock_noreport
[ 49.712712] ok 23 - test_atomic_builtins
[ 49.746428] ok 24 - test_1bit_value_change
[ 49.753316] ok 1 - kcsan

Tested-by: Nathan Chancellor <[email protected]>

> ---
> arch/s390/include/asm/stacktrace.h | 20 ++++++++++----------
> arch/s390/include/asm/unwind.h | 8 ++++----
> 2 files changed, 14 insertions(+), 14 deletions(-)
>
> diff --git a/arch/s390/include/asm/stacktrace.h b/arch/s390/include/asm/stacktrace.h
> index 3d8a4b94c620..22c41d7fd95c 100644
> --- a/arch/s390/include/asm/stacktrace.h
> +++ b/arch/s390/include/asm/stacktrace.h
> @@ -34,16 +34,6 @@ static inline bool on_stack(struct stack_info *info,
> return addr >= info->begin && addr + len <= info->end;
> }
>
> -static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
> - struct pt_regs *regs)
> -{
> - if (regs)
> - return (unsigned long) kernel_stack_pointer(regs);
> - if (task == current)
> - return current_stack_pointer();
> - return (unsigned long) task->thread.ksp;
> -}
> -
> /*
> * Stack layout of a C stack frame.
> */
> @@ -74,6 +64,16 @@ struct stack_frame {
> ((unsigned long)__builtin_frame_address(0) - \
> offsetof(struct stack_frame, back_chain))
>
> +static __always_inline unsigned long get_stack_pointer(struct task_struct *task,
> + struct pt_regs *regs)
> +{
> + if (regs)
> + return (unsigned long) kernel_stack_pointer(regs);
> + if (task == current)
> + return current_frame_address();
> + return (unsigned long) task->thread.ksp;
> +}
> +
> /*
> * To keep this simple mark register 2-6 as being changed (volatile)
> * by the called function, even though register 6 is saved/nonvolatile.
> diff --git a/arch/s390/include/asm/unwind.h b/arch/s390/include/asm/unwind.h
> index de9006b0cfeb..5ebf534ef753 100644
> --- a/arch/s390/include/asm/unwind.h
> +++ b/arch/s390/include/asm/unwind.h
> @@ -55,10 +55,10 @@ static inline bool unwind_error(struct unwind_state *state)
> return state->error;
> }
>
> -static inline void unwind_start(struct unwind_state *state,
> - struct task_struct *task,
> - struct pt_regs *regs,
> - unsigned long first_frame)
> +static __always_inline void unwind_start(struct unwind_state *state,
> + struct task_struct *task,
> + struct pt_regs *regs,
> + unsigned long first_frame)
> {
> task = task ?: current;
> first_frame = first_frame ?: get_stack_pointer(task, regs);
> --
> 2.25.4