Re: [PATCH v10 02/14] unwind_user: Add frame pointer support

2025-06-24 Thread Steven Rostedt
On Mon, 23 Jun 2025 09:31:17 -0700
Indu Bhagat  wrote:

> >> Also, CFA here is Call-Frame-Address and RA Return-Address ?  
> > 
> > I believe so. Do you want me to add a comment?
> >   
> 
> If a comment is added, Canonical Frame Address will be more appropriate.

Thanks Indu,

Updated.

-- Steve



Re: [PATCH v10 02/14] unwind_user: Add frame pointer support

2025-06-23 Thread Indu Bhagat

On 6/18/25 8:09 AM, Steven Rostedt wrote:

On Wed, 18 Jun 2025 15:52:01 +0200
Peter Zijlstra  wrote:


+the_end:
+   state->done = true;
return -EINVAL;
  }


I'm thinking 'the_end' might be better named 'done' ?


I thought it was cute ;-) (BTW, I didn't name it).

But sure, I can update it to be something more common.



Also, CFA here is Call-Frame-Address and RA Return-Address ?


I believe so. Do you want me to add a comment?



If a comment is added, Canonical Frame Address will be more appropriate.




Re: [PATCH v10 02/14] unwind_user: Add frame pointer support

2025-06-18 Thread Steven Rostedt
On Wed, 18 Jun 2025 15:52:01 +0200
Peter Zijlstra  wrote:

> > +the_end:
> > +   state->done = true;
> > return -EINVAL;
> >  }  
> 
> I'm thinking 'the_end' might be better named 'done' ?

I thought it was cute ;-) (BTW, I didn't name it).

But sure, I can update it to be something more common.

> 
> Also, CFA here is Call-Frame-Address and RA Return-Address ?

I believe so. Do you want me to add a comment?

-- Steve



Re: [PATCH v10 02/14] unwind_user: Add frame pointer support

2025-06-18 Thread Peter Zijlstra
On Tue, Jun 10, 2025 at 08:54:23PM -0400, Steven Rostedt wrote:
>  int unwind_user_next(struct unwind_user_state *state)
>  {
> + struct unwind_user_frame *frame;
> + unsigned long cfa = 0, fp, ra = 0;
> +
> + if (state->done)
> + return -EINVAL;
> +
> + if (fp_state(state))
> + frame = &fp_frame;
> + else
> + goto the_end;
> +
> + cfa = (frame->use_fp ? state->fp : state->sp) + frame->cfa_off;
> +
> + /* stack going in wrong direction? */
> + if (cfa <= state->sp)
> + goto the_end;
> +
> + if (get_user(ra, (unsigned long *)(cfa + frame->ra_off)))
> + goto the_end;
> +
> + if (frame->fp_off && get_user(fp, (unsigned long __user *)(cfa + 
> frame->fp_off)))
> + goto the_end;
> +
> + state->ip = ra;
> + state->sp = cfa;
> + if (frame->fp_off)
> + state->fp = fp;
> +
> + return 0;
> +
> +the_end:
> + state->done = true;
>   return -EINVAL;
>  }

I'm thinking 'the_end' might be better named 'done' ?

Also, CFA here is Call-Frame-Address and RA Return-Address ?



[PATCH v10 02/14] unwind_user: Add frame pointer support

2025-06-10 Thread Steven Rostedt
From: Josh Poimboeuf 

Add optional support for user space frame pointer unwinding.  If
supported, the arch needs to enable CONFIG_HAVE_UNWIND_USER_FP and
define ARCH_INIT_USER_FP_FRAME.

By encoding the frame offsets in struct unwind_user_frame, much of this
code can also be reused for future unwinder implementations like sframe.

Signed-off-by: Josh Poimboeuf 
Co-developed-by: Steven Rostedt (Google) 
Signed-off-by: Steven Rostedt (Google) 
---
Changes since v9: 
https://lore.kernel.org/linux-trace-kernel/20250513223551.290698...@goodmis.org/

As asm-generic headers are not included when an architecture defines the
header, having more than one #ifndef and setting variables does not work
with those checks in the asm-generic header and the architecture header
does not define all the values.

- Move ARCH_INIT_USER_FP_FRAME check to linux/user_unwind.h

- Have linux/user_unwind.h include asm/user_unwind.h and not have C files
  have to call the asm header directly

- Remove unnecessary frame initialization

- Added unwind_user.h to asm-generic/Kbuild

 arch/Kconfig  |  4 +++
 include/asm-generic/Kbuild|  1 +
 include/asm-generic/unwind_user.h |  5 
 include/linux/unwind_user.h   |  5 
 include/linux/unwind_user_types.h |  1 +
 kernel/unwind/user.c  | 49 +--
 6 files changed, 63 insertions(+), 2 deletions(-)
 create mode 100644 include/asm-generic/unwind_user.h

diff --git a/arch/Kconfig b/arch/Kconfig
index ea59e5d7cc69..8e3fd723bd74 100644
--- a/arch/Kconfig
+++ b/arch/Kconfig
@@ -438,6 +438,10 @@ config HAVE_HARDLOCKUP_DETECTOR_ARCH
 config UNWIND_USER
bool
 
+config HAVE_UNWIND_USER_FP
+   bool
+   select UNWIND_USER
+
 config HAVE_PERF_REGS
bool
help
diff --git a/include/asm-generic/Kbuild b/include/asm-generic/Kbuild
index 8675b7b4ad23..295c94a3ccc1 100644
--- a/include/asm-generic/Kbuild
+++ b/include/asm-generic/Kbuild
@@ -59,6 +59,7 @@ mandatory-y += tlbflush.h
 mandatory-y += topology.h
 mandatory-y += trace_clock.h
 mandatory-y += uaccess.h
+mandatory-y += unwind_user.h
 mandatory-y += vermagic.h
 mandatory-y += vga.h
 mandatory-y += video.h
diff --git a/include/asm-generic/unwind_user.h 
b/include/asm-generic/unwind_user.h
new file mode 100644
index ..b8882b909944
--- /dev/null
+++ b/include/asm-generic/unwind_user.h
@@ -0,0 +1,5 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_UNWIND_USER_H
+#define _ASM_GENERIC_UNWIND_USER_H
+
+#endif /* _ASM_GENERIC_UNWIND_USER_H */
diff --git a/include/linux/unwind_user.h b/include/linux/unwind_user.h
index aa7923c1384f..a405111c41b0 100644
--- a/include/linux/unwind_user.h
+++ b/include/linux/unwind_user.h
@@ -3,6 +3,11 @@
 #define _LINUX_UNWIND_USER_H
 
 #include 
+#include 
+
+#ifndef ARCH_INIT_USER_FP_FRAME
+ #define ARCH_INIT_USER_FP_FRAME
+#endif
 
 int unwind_user_start(struct unwind_user_state *state);
 int unwind_user_next(struct unwind_user_state *state);
diff --git a/include/linux/unwind_user_types.h 
b/include/linux/unwind_user_types.h
index 6ed1b4ae74e1..65bd070eb6b0 100644
--- a/include/linux/unwind_user_types.h
+++ b/include/linux/unwind_user_types.h
@@ -6,6 +6,7 @@
 
 enum unwind_user_type {
UNWIND_USER_TYPE_NONE,
+   UNWIND_USER_TYPE_FP,
 };
 
 struct unwind_stacktrace {
diff --git a/kernel/unwind/user.c b/kernel/unwind/user.c
index d30449328981..4fc550356b33 100644
--- a/kernel/unwind/user.c
+++ b/kernel/unwind/user.c
@@ -6,10 +6,52 @@
 #include 
 #include 
 #include 
+#include 
+
+static struct unwind_user_frame fp_frame = {
+   ARCH_INIT_USER_FP_FRAME
+};
+
+static inline bool fp_state(struct unwind_user_state *state)
+{
+   return IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP) &&
+  state->type == UNWIND_USER_TYPE_FP;
+}
 
 int unwind_user_next(struct unwind_user_state *state)
 {
-   /* no implementation yet */
+   struct unwind_user_frame *frame;
+   unsigned long cfa = 0, fp, ra = 0;
+
+   if (state->done)
+   return -EINVAL;
+
+   if (fp_state(state))
+   frame = &fp_frame;
+   else
+   goto the_end;
+
+   cfa = (frame->use_fp ? state->fp : state->sp) + frame->cfa_off;
+
+   /* stack going in wrong direction? */
+   if (cfa <= state->sp)
+   goto the_end;
+
+   if (get_user(ra, (unsigned long *)(cfa + frame->ra_off)))
+   goto the_end;
+
+   if (frame->fp_off && get_user(fp, (unsigned long __user *)(cfa + 
frame->fp_off)))
+   goto the_end;
+
+   state->ip = ra;
+   state->sp = cfa;
+   if (frame->fp_off)
+   state->fp = fp;
+
+   return 0;
+
+the_end:
+   state->done = true;
return -EINVAL;
 }
 
@@ -24,7 +66,10 @@ int unwind_user_start(struct unwind_user_state *state)
return -EINVAL;
}
 
-   state->type = UNWIND_USER_TYPE_NONE;
+   if (IS_ENABLED(CONFIG_HAVE_UNWIND_USER_FP))
+