[RESEND,v8,0/9] LoongArch: Add ftrace support

Message ID 20221116082338.5145-1-zhangqing@loongson.cn
Headers
Series LoongArch: Add ftrace support |

Message

Qing Zhang Nov. 16, 2022, 8:23 a.m. UTC
  This patch series to support basic and dynamic ftrace.

1) -pg
Use `-pg` makes stub like a child function `void _mcount(void *ra)`.
Thus, it can be seen store RA and open stack before `call _mcount`.
Find `open stack` at first, and then find `store RA`.

2) -fpatchable-function-entry=2
The compiler has inserted 2 NOPs before the regular function prologue.
T series registers are available and safe because of LoongArch psABI.

At runtime, replace nop with bl to enable ftrace call and replace bl with
nop to disable ftrace call. The bl requires us to save the original RA value,
so here it saves RA at t0.
details are:

| Compiled   |       Disabled         |        Enabled         |
+------------+------------------------+------------------------+
| nop        | move     t0, ra        | move     t0, ra        |
| nop        | nop                    | bl      ftrace_caller  |
| func_body  | func_body              | func_body              |

The RA value will be recovered by ftrace_regs_entry, and restored into RA
before returning to the regular function prologue. When a function is not
being traced, the move t0, ra is not harmful.

performs a series of startup tests on ftrace and The test cases in selftests
has passed on LoongArch.

Changes in v2:
 - Remove patch "LoongArch: ftrace: Add CALLER_ADDRx macros" there are other
   better ways
 Suggested by Steve:
 - Add HAVE_DYNAMIC_FTRACE_WITH_ARGS support (6/10)
 Suggested by Jinyang:
 - Change addu16id to lu12iw and Adjust module_finalize return value (7/10)
 - Use the "jr" pseudo-instruction where applicable (1/10)
 - Use the "la.pcrel" instead of "la" (3/10)

Changes in v3:
 Reported by Jeff:
 - Fix unwind state when option func_stack_trace (10/10)

Changes in v4:
 - No comments. Just resend the series.
 - Rebased onto v6.0.0-rc4.

Changes in v5:
 - Modify the indentation of Kconfig and small changes

Changes in v6:
 Suggested by Huacai:
 - Adjusting the patch Sequence
 - renamed mcount-dyn.S for consistency

Changes in v7:
 - delete redefinition

Changes in v8:
 - remove useless macro judgment and modify the return location
 - move some code to Patch-3

Qing Zhang (9):
  LoongArch/ftrace: Add basic support
  LoongArch/ftrace: Add recordmcount support
  LoongArch/ftrace: Add dynamic function tracer support
  LoongArch/ftrace: Add dynamic function graph tracer support
  LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_REGS support
  LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_ARGS support
  LoongArch/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support
  LoongArch: modules/ftrace: Initialize PLT at load time
  LoongArch: Enable CONFIG_KALLSYMS_ALL and CONFIG_DEBUG_FS

 arch/loongarch/Kconfig                     |   7 +
 arch/loongarch/Makefile                    |   5 +
 arch/loongarch/configs/loongson3_defconfig |   2 +
 arch/loongarch/include/asm/ftrace.h        |  59 +++++
 arch/loongarch/include/asm/inst.h          |  15 ++
 arch/loongarch/include/asm/module.h        |   5 +-
 arch/loongarch/include/asm/module.lds.h    |   1 +
 arch/loongarch/include/asm/unwind.h        |   3 +-
 arch/loongarch/kernel/Makefile             |  13 +
 arch/loongarch/kernel/ftrace.c             |  74 ++++++
 arch/loongarch/kernel/ftrace_dyn.c         | 264 +++++++++++++++++++++
 arch/loongarch/kernel/inst.c               | 127 ++++++++++
 arch/loongarch/kernel/mcount.S             |  94 ++++++++
 arch/loongarch/kernel/mcount_dyn.S         | 154 ++++++++++++
 arch/loongarch/kernel/module-sections.c    |  11 +
 arch/loongarch/kernel/module.c             |  21 ++
 arch/loongarch/kernel/unwind_guess.c       |   4 +-
 arch/loongarch/kernel/unwind_prologue.c    |  46 +++-
 scripts/recordmcount.c                     |  23 ++
 19 files changed, 918 insertions(+), 10 deletions(-)
 create mode 100644 arch/loongarch/include/asm/ftrace.h
 create mode 100644 arch/loongarch/kernel/ftrace.c
 create mode 100644 arch/loongarch/kernel/ftrace_dyn.c
 create mode 100644 arch/loongarch/kernel/mcount.S
 create mode 100644 arch/loongarch/kernel/mcount_dyn.S
  

Comments

Huacai Chen Nov. 19, 2022, 4:55 a.m. UTC | #1
Hi, Qing,

I have applied this series to
https://github.com/loongson/linux/commits/loongarch-next with some
slight modifications, please test that branch to see if everything is
correct.

Huacai

On Wed, Nov 16, 2022 at 4:23 PM Qing Zhang <zhangqing@loongson.cn> wrote:
>
> This patch series to support basic and dynamic ftrace.
>
> 1) -pg
> Use `-pg` makes stub like a child function `void _mcount(void *ra)`.
> Thus, it can be seen store RA and open stack before `call _mcount`.
> Find `open stack` at first, and then find `store RA`.
>
> 2) -fpatchable-function-entry=2
> The compiler has inserted 2 NOPs before the regular function prologue.
> T series registers are available and safe because of LoongArch psABI.
>
> At runtime, replace nop with bl to enable ftrace call and replace bl with
> nop to disable ftrace call. The bl requires us to save the original RA value,
> so here it saves RA at t0.
> details are:
>
> | Compiled   |       Disabled         |        Enabled         |
> +------------+------------------------+------------------------+
> | nop        | move     t0, ra        | move     t0, ra        |
> | nop        | nop                    | bl      ftrace_caller  |
> | func_body  | func_body              | func_body              |
>
> The RA value will be recovered by ftrace_regs_entry, and restored into RA
> before returning to the regular function prologue. When a function is not
> being traced, the move t0, ra is not harmful.
>
> performs a series of startup tests on ftrace and The test cases in selftests
> has passed on LoongArch.
>
> Changes in v2:
>  - Remove patch "LoongArch: ftrace: Add CALLER_ADDRx macros" there are other
>    better ways
>  Suggested by Steve:
>  - Add HAVE_DYNAMIC_FTRACE_WITH_ARGS support (6/10)
>  Suggested by Jinyang:
>  - Change addu16id to lu12iw and Adjust module_finalize return value (7/10)
>  - Use the "jr" pseudo-instruction where applicable (1/10)
>  - Use the "la.pcrel" instead of "la" (3/10)
>
> Changes in v3:
>  Reported by Jeff:
>  - Fix unwind state when option func_stack_trace (10/10)
>
> Changes in v4:
>  - No comments. Just resend the series.
>  - Rebased onto v6.0.0-rc4.
>
> Changes in v5:
>  - Modify the indentation of Kconfig and small changes
>
> Changes in v6:
>  Suggested by Huacai:
>  - Adjusting the patch Sequence
>  - renamed mcount-dyn.S for consistency
>
> Changes in v7:
>  - delete redefinition
>
> Changes in v8:
>  - remove useless macro judgment and modify the return location
>  - move some code to Patch-3
>
> Qing Zhang (9):
>   LoongArch/ftrace: Add basic support
>   LoongArch/ftrace: Add recordmcount support
>   LoongArch/ftrace: Add dynamic function tracer support
>   LoongArch/ftrace: Add dynamic function graph tracer support
>   LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_REGS support
>   LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_ARGS support
>   LoongArch/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support
>   LoongArch: modules/ftrace: Initialize PLT at load time
>   LoongArch: Enable CONFIG_KALLSYMS_ALL and CONFIG_DEBUG_FS
>
>  arch/loongarch/Kconfig                     |   7 +
>  arch/loongarch/Makefile                    |   5 +
>  arch/loongarch/configs/loongson3_defconfig |   2 +
>  arch/loongarch/include/asm/ftrace.h        |  59 +++++
>  arch/loongarch/include/asm/inst.h          |  15 ++
>  arch/loongarch/include/asm/module.h        |   5 +-
>  arch/loongarch/include/asm/module.lds.h    |   1 +
>  arch/loongarch/include/asm/unwind.h        |   3 +-
>  arch/loongarch/kernel/Makefile             |  13 +
>  arch/loongarch/kernel/ftrace.c             |  74 ++++++
>  arch/loongarch/kernel/ftrace_dyn.c         | 264 +++++++++++++++++++++
>  arch/loongarch/kernel/inst.c               | 127 ++++++++++
>  arch/loongarch/kernel/mcount.S             |  94 ++++++++
>  arch/loongarch/kernel/mcount_dyn.S         | 154 ++++++++++++
>  arch/loongarch/kernel/module-sections.c    |  11 +
>  arch/loongarch/kernel/module.c             |  21 ++
>  arch/loongarch/kernel/unwind_guess.c       |   4 +-
>  arch/loongarch/kernel/unwind_prologue.c    |  46 +++-
>  scripts/recordmcount.c                     |  23 ++
>  19 files changed, 918 insertions(+), 10 deletions(-)
>  create mode 100644 arch/loongarch/include/asm/ftrace.h
>  create mode 100644 arch/loongarch/kernel/ftrace.c
>  create mode 100644 arch/loongarch/kernel/ftrace_dyn.c
>  create mode 100644 arch/loongarch/kernel/mcount.S
>  create mode 100644 arch/loongarch/kernel/mcount_dyn.S
>
> --
> 2.36.0
>
>
  
Jeff Xie Nov. 25, 2022, 5:02 a.m. UTC | #2
On Sat, Nov 19, 2022 at 1:21 PM Huacai Chen <chenhuacai@kernel.org> wrote:
>
> Hi, Qing,
>
> I have applied this series to
> https://github.com/loongson/linux/commits/loongarch-next with some
> slight modifications, please test that branch to see if everything is
> correct.

I found the call trace relative issue for the branch:
https://github.com/loongson/linux/commits/loongarch-next

test01:

[root@loongarch tracing]# echo do_sys_openat2 > ./set_ftrace_filter
[root@loongarch tracing]# echo 1 > ./options/func_stack_trace
[root@loongarch tracing]# echo function > ./current_tracer
[root@loongarch tracing]# cat ./trace
# tracer: function
#
# entries-in-buffer/entries-written: 6/6   #P:1
#
#                                _-----=> irqs-off/BH-disabled
#                               / _----=> need-resched
#                              | / _---=> hardirq/softirq
#                              || / _--=> preempt-depth
#                              ||| / _-=> migrate-disable
#                              |||| /     delay
#           TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
#              | |         |   |||||     |         |
              sh-84      [000] .....    37.784924: do_sys_openat2 <-sys_openat
              sh-84      [000] .....    37.786382: <stack trace>
 => sys_openat
 => do_syscall
 => handle_syscall
 => 0                  // abnormal output

test02:

[root@loongarch tracing]# echo do_sys_openat2:stacktrace > ./set_ftrace_filter
[root@loongarch tracing]# cat ./trace
# tracer: nop
#
# entries-in-buffer/entries-written: 3/3   #P:1
#
#                                _-----=> irqs-off/BH-disabled
#                               / _----=> need-resched
#                              | / _---=> hardirq/softirq
#                              || / _--=> preempt-depth
#                              ||| / _-=> migrate-disable
#                              |||| /     delay
#           TASK-PID     CPU#  |||||  TIMESTAMP  FUNCTION
#              | |         |   |||||     |         |
              sh-84      [000] .n.2.   107.946986: <stack trace>
 => do_syscall
 => handle_syscall
 => 0xd9ad812ae
              sh-84      [000] ...2.   110.584354: <stack trace>
 => do_syscall
 => handle_syscall
 => 0xd9ad812ae    // abnormal output

test03:

[root@loongarch tracing]# echo 1 > ./options/stacktrace
[root@loongarch tracing]# echo 1 > ./events/sched/sched_switch/enable
[root@loongarch tracing]# cat ./trace
...
<idle>-0       [000] d..2.    27.981495: sched_switch:
prev_comm=swapper/0 prev_pid=0 prev_prio=120 prev_state=R ==>
next_comm=rcu_preempt next
_pid=15 next_prio=120
          <idle>-0       [000] d..2.    27.981508: <stack trace>
 => __schedule
 => schedule_idle
 => cpu_startup_entry
 => kernel_init
 => 0x0074706d   // abnormal output



>
> Huacai
>
> On Wed, Nov 16, 2022 at 4:23 PM Qing Zhang <zhangqing@loongson.cn> wrote:
> >
> > This patch series to support basic and dynamic ftrace.
> >
> > 1) -pg
> > Use `-pg` makes stub like a child function `void _mcount(void *ra)`.
> > Thus, it can be seen store RA and open stack before `call _mcount`.
> > Find `open stack` at first, and then find `store RA`.
> >
> > 2) -fpatchable-function-entry=2
> > The compiler has inserted 2 NOPs before the regular function prologue.
> > T series registers are available and safe because of LoongArch psABI.
> >
> > At runtime, replace nop with bl to enable ftrace call and replace bl with
> > nop to disable ftrace call. The bl requires us to save the original RA value,
> > so here it saves RA at t0.
> > details are:
> >
> > | Compiled   |       Disabled         |        Enabled         |
> > +------------+------------------------+------------------------+
> > | nop        | move     t0, ra        | move     t0, ra        |
> > | nop        | nop                    | bl      ftrace_caller  |
> > | func_body  | func_body              | func_body              |
> >
> > The RA value will be recovered by ftrace_regs_entry, and restored into RA
> > before returning to the regular function prologue. When a function is not
> > being traced, the move t0, ra is not harmful.
> >
> > performs a series of startup tests on ftrace and The test cases in selftests
> > has passed on LoongArch.
> >
> > Changes in v2:
> >  - Remove patch "LoongArch: ftrace: Add CALLER_ADDRx macros" there are other
> >    better ways
> >  Suggested by Steve:
> >  - Add HAVE_DYNAMIC_FTRACE_WITH_ARGS support (6/10)
> >  Suggested by Jinyang:
> >  - Change addu16id to lu12iw and Adjust module_finalize return value (7/10)
> >  - Use the "jr" pseudo-instruction where applicable (1/10)
> >  - Use the "la.pcrel" instead of "la" (3/10)
> >
> > Changes in v3:
> >  Reported by Jeff:
> >  - Fix unwind state when option func_stack_trace (10/10)
> >
> > Changes in v4:
> >  - No comments. Just resend the series.
> >  - Rebased onto v6.0.0-rc4.
> >
> > Changes in v5:
> >  - Modify the indentation of Kconfig and small changes
> >
> > Changes in v6:
> >  Suggested by Huacai:
> >  - Adjusting the patch Sequence
> >  - renamed mcount-dyn.S for consistency
> >
> > Changes in v7:
> >  - delete redefinition
> >
> > Changes in v8:
> >  - remove useless macro judgment and modify the return location
> >  - move some code to Patch-3
> >
> > Qing Zhang (9):
> >   LoongArch/ftrace: Add basic support
> >   LoongArch/ftrace: Add recordmcount support
> >   LoongArch/ftrace: Add dynamic function tracer support
> >   LoongArch/ftrace: Add dynamic function graph tracer support
> >   LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_REGS support
> >   LoongArch/ftrace: Add HAVE_DYNAMIC_FTRACE_WITH_ARGS support
> >   LoongArch/ftrace: Add HAVE_FUNCTION_GRAPH_RET_ADDR_PTR support
> >   LoongArch: modules/ftrace: Initialize PLT at load time
> >   LoongArch: Enable CONFIG_KALLSYMS_ALL and CONFIG_DEBUG_FS
> >
> >  arch/loongarch/Kconfig                     |   7 +
> >  arch/loongarch/Makefile                    |   5 +
> >  arch/loongarch/configs/loongson3_defconfig |   2 +
> >  arch/loongarch/include/asm/ftrace.h        |  59 +++++
> >  arch/loongarch/include/asm/inst.h          |  15 ++
> >  arch/loongarch/include/asm/module.h        |   5 +-
> >  arch/loongarch/include/asm/module.lds.h    |   1 +
> >  arch/loongarch/include/asm/unwind.h        |   3 +-
> >  arch/loongarch/kernel/Makefile             |  13 +
> >  arch/loongarch/kernel/ftrace.c             |  74 ++++++
> >  arch/loongarch/kernel/ftrace_dyn.c         | 264 +++++++++++++++++++++
> >  arch/loongarch/kernel/inst.c               | 127 ++++++++++
> >  arch/loongarch/kernel/mcount.S             |  94 ++++++++
> >  arch/loongarch/kernel/mcount_dyn.S         | 154 ++++++++++++
> >  arch/loongarch/kernel/module-sections.c    |  11 +
> >  arch/loongarch/kernel/module.c             |  21 ++
> >  arch/loongarch/kernel/unwind_guess.c       |   4 +-
> >  arch/loongarch/kernel/unwind_prologue.c    |  46 +++-
> >  scripts/recordmcount.c                     |  23 ++
> >  19 files changed, 918 insertions(+), 10 deletions(-)
> >  create mode 100644 arch/loongarch/include/asm/ftrace.h
> >  create mode 100644 arch/loongarch/kernel/ftrace.c
> >  create mode 100644 arch/loongarch/kernel/ftrace_dyn.c
> >  create mode 100644 arch/loongarch/kernel/mcount.S
> >  create mode 100644 arch/loongarch/kernel/mcount_dyn.S
> >
> > --
> > 2.36.0
> >
> >