[PATCH] riscv: ftrace: Fix to pass correct ftrace_regs to ftrace_func_t functions

2023-10-02 Thread Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) 

Since ftrace_func_t requires to pass 'struct ftrace_regs *' as the 4th
argument even if FTRACE_OPS_FL_SAVE_REGS is not set, ftrace_caller must
pass 'struct ftrace_regs *', which is a partial pt_regs, on the stack
to the ftrace_func_t functions, so that the ftrace_func_t functions can
access some partial registers.

Fix to allocate 'struct ftrace_regs' (which has the same size of 'struct
pt_regs') on the stack and save partial (argument) registers on it
instead of reduced size custom data structure.

Fixes: afc76b8b8011 ("riscv: Using PATCHABLE_FUNCTION_ENTRY instead of MCOUNT")
Signed-off-by: Masami Hiramatsu (Google) 
---
 arch/riscv/kernel/mcount-dyn.S |   65 +---
 1 file changed, 28 insertions(+), 37 deletions(-)

diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S
index 669b8697aa38..84963680eff4 100644
--- a/arch/riscv/kernel/mcount-dyn.S
+++ b/arch/riscv/kernel/mcount-dyn.S
@@ -14,46 +14,37 @@
.text
 
 #define FENTRY_RA_OFFSET   8
-#define ABI_SIZE_ON_STACK  80
-#define ABI_A0 0
-#define ABI_A1 8
-#define ABI_A2 16
-#define ABI_A3 24
-#define ABI_A4 32
-#define ABI_A5 40
-#define ABI_A6 48
-#define ABI_A7 56
-#define ABI_T0 64
-#define ABI_RA 72
 
.macro SAVE_ABI
-   addisp, sp, -ABI_SIZE_ON_STACK
-
-   REG_S   a0, ABI_A0(sp)
-   REG_S   a1, ABI_A1(sp)
-   REG_S   a2, ABI_A2(sp)
-   REG_S   a3, ABI_A3(sp)
-   REG_S   a4, ABI_A4(sp)
-   REG_S   a5, ABI_A5(sp)
-   REG_S   a6, ABI_A6(sp)
-   REG_S   a7, ABI_A7(sp)
-   REG_S   t0, ABI_T0(sp)
-   REG_S   ra, ABI_RA(sp)
+   addisp, sp, -PT_SIZE_ON_STACK
+
+   /* Save t0 as epc for ftrace_regs_get_instruction_pointer() */
+   REG_S   t0, PT_EPC(sp)
+   REG_S   a0, PT_A0(sp)
+   REG_S   a1, PT_A1(sp)
+   REG_S   a2, PT_A2(sp)
+   REG_S   a3, PT_A3(sp)
+   REG_S   a4, PT_A4(sp)
+   REG_S   a5, PT_A5(sp)
+   REG_S   a6, PT_A6(sp)
+   REG_S   a7, PT_A7(sp)
+   REG_S   t0, PT_T0(sp)
+   REG_S   ra, PT_RA(sp)
.endm
 
.macro RESTORE_ABI
-   REG_L   a0, ABI_A0(sp)
-   REG_L   a1, ABI_A1(sp)
-   REG_L   a2, ABI_A2(sp)
-   REG_L   a3, ABI_A3(sp)
-   REG_L   a4, ABI_A4(sp)
-   REG_L   a5, ABI_A5(sp)
-   REG_L   a6, ABI_A6(sp)
-   REG_L   a7, ABI_A7(sp)
-   REG_L   t0, ABI_T0(sp)
-   REG_L   ra, ABI_RA(sp)
-
-   addisp, sp, ABI_SIZE_ON_STACK
+   REG_L   a0, PT_A0(sp)
+   REG_L   a1, PT_A1(sp)
+   REG_L   a2, PT_A2(sp)
+   REG_L   a3, PT_A3(sp)
+   REG_L   a4, PT_A4(sp)
+   REG_L   a5, PT_A5(sp)
+   REG_L   a6, PT_A6(sp)
+   REG_L   a7, PT_A7(sp)
+   REG_L   t0, PT_T0(sp)
+   REG_L   ra, PT_RA(sp)
+
+   addisp, sp, PT_SIZE_ON_STACK
.endm
 
 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
@@ -96,8 +87,8 @@ ftrace_call:
callftrace_stub
 
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
-   addia0, sp, ABI_RA
-   REG_L   a1, ABI_T0(sp)
+   addia0, sp, PT_RA
+   REG_L   a1, PT_T0(sp)
addia1, a1, -FENTRY_RA_OFFSET
 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
mv  a2, s0




[PATCH] tracing: probes: Fix to zero initialize a local variable

2024-03-20 Thread Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) 

Fix to initialize 'val' local variable with zero.
Dan reported that Smatch static code checker reports an error that a local
'val' variable needs to be initialized. Actually, the 'val' is expected to
be initialized by FETCH_OP_ARG in the same loop, but it is not obvious. So
initialize it with zero.

Reported-by: Dan Carpenter 
Closes: 
https://lore.kernel.org/all/b010488e-68aa-407c-add0-3e059254aaa0@moroto.mountain/
Fixes: 25f00e40ce79 ("tracing/probes: Support $argN in return probe (kprobe and 
fprobe)")
Signed-off-by: Masami Hiramatsu (Google) 
---
 kernel/trace/trace_probe.c |2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
index 217169de0920..dfe3ee6035ec 100644
--- a/kernel/trace/trace_probe.c
+++ b/kernel/trace/trace_probe.c
@@ -839,7 +839,7 @@ int traceprobe_get_entry_data_size(struct trace_probe *tp)
 void store_trace_entry_data(void *edata, struct trace_probe *tp, struct 
pt_regs *regs)
 {
struct probe_entry_arg *earg = tp->entry_arg;
-   unsigned long val;
+   unsigned long val = 0;
int i;
 
if (!earg)




[PATCH] selftests/ftrace: Fix eventfs ownership testcase to find mount point

2024-09-04 Thread Masami Hiramatsu (Google)
From: Masami Hiramatsu (Google) 

Fix eventfs ownership testcase to find mount point if stat -c "%m" failed.
This can happen on the system based on busybox. In this case, this will
try to use the current working directory, which should be a tracefs top
directory (and eventfs is mounted as a part of tracefs.)
If it does not work, the test is skipped as UNRESOLVED because of
the environmental problem.

Fixes: ee9793be08b1 ("tracing/selftests: Add ownership modification tests for 
eventfs")
Signed-off-by: Masami Hiramatsu (Google) 
---
 .../ftrace/test.d/00basic/test_ownership.tc|   12 
 1 file changed, 12 insertions(+)

diff --git a/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc 
b/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc
index c45094d1e1d2..803efd7b56c7 100644
--- a/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc
+++ b/tools/testing/selftests/ftrace/test.d/00basic/test_ownership.tc
@@ -6,6 +6,18 @@ original_group=`stat -c "%g" .`
 original_owner=`stat -c "%u" .`
 
 mount_point=`stat -c '%m' .`
+
+# If stat -c '%m' does not work (e.g. busybox) or failed, try to use the
+# current working directory (which should be a tracefs) as the mount point.
+if [ ! -d "$mount_point" ]; then
+   if mount | grep -qw $PWD ; then
+   mount_point=$PWD
+   else
+   # If PWD doesn't work, that is an environmental problem.
+   exit_unresolved
+   fi
+fi
+
 mount_options=`mount | grep "$mount_point" | sed -e 's/.*(\(.*\)).*/\1/'`
 
 # find another owner and group that is not the original