This is a note to let you know that I've just added the patch titled ftrace: Fix DIRECT_CALLS to use SAVE_REGS by default to the 6.7-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: ftrace-fix-direct_calls-to-use-save_regs-by-default.patch and it can be found in the queue-6.7 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From a8b9cf62ade1bf17261a979fc97e40c2d7842353 Mon Sep 17 00:00:00 2001 From: "Masami Hiramatsu (Google)" <mhiramat@xxxxxxxxxx> Date: Wed, 10 Jan 2024 09:13:06 +0900 Subject: ftrace: Fix DIRECT_CALLS to use SAVE_REGS by default From: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx> commit a8b9cf62ade1bf17261a979fc97e40c2d7842353 upstream. The commit 60c8971899f3 ("ftrace: Make DIRECT_CALLS work WITH_ARGS and !WITH_REGS") changed DIRECT_CALLS to use SAVE_ARGS when there are multiple ftrace_ops at the same function, but since the x86 only support to jump to direct_call from ftrace_regs_caller, when we set the function tracer on the same target function on x86, ftrace-direct does not work as below (this actually works on arm64.) At first, insmod ftrace-direct.ko to put a direct_call on 'wake_up_process()'. # insmod kernel/samples/ftrace/ftrace-direct.ko # less trace .. <idle>-0 [006] ..s1. 564.686958: my_direct_func: waking up rcu_preempt-17 <idle>-0 [007] ..s1. 564.687836: my_direct_func: waking up kcompactd0-63 <idle>-0 [006] ..s1. 564.690926: my_direct_func: waking up rcu_preempt-17 <idle>-0 [006] ..s1. 564.696872: my_direct_func: waking up rcu_preempt-17 <idle>-0 [007] ..s1. 565.191982: my_direct_func: waking up kcompactd0-63 Setup a function filter to the 'wake_up_process' too, and enable it. # cd /sys/kernel/tracing/ # echo wake_up_process > set_ftrace_filter # echo function > current_tracer # less trace .. <idle>-0 [006] ..s3. 686.180972: wake_up_process <-call_timer_fn <idle>-0 [006] ..s3. 686.186919: wake_up_process <-call_timer_fn <idle>-0 [002] ..s3. 686.264049: wake_up_process <-call_timer_fn <idle>-0 [002] d.h6. 686.515216: wake_up_process <-kick_pool <idle>-0 [002] d.h6. 686.691386: wake_up_process <-kick_pool Then, only function tracer is shown on x86. But if you enable 'kprobe on ftrace' event (which uses SAVE_REGS flag) on the same function, it is shown again. # echo 'p wake_up_process' >> dynamic_events # echo 1 > events/kprobes/p_wake_up_process_0/enable # echo > trace # less trace .. <idle>-0 [006] ..s2. 2710.345919: p_wake_up_process_0: (wake_up_process+0x4/0x20) <idle>-0 [006] ..s3. 2710.345923: wake_up_process <-call_timer_fn <idle>-0 [006] ..s1. 2710.345928: my_direct_func: waking up rcu_preempt-17 <idle>-0 [006] ..s2. 2710.349931: p_wake_up_process_0: (wake_up_process+0x4/0x20) <idle>-0 [006] ..s3. 2710.349934: wake_up_process <-call_timer_fn <idle>-0 [006] ..s1. 2710.349937: my_direct_func: waking up rcu_preempt-17 To fix this issue, use SAVE_REGS flag for multiple ftrace_ops flag of direct_call by default. Link: https://lore.kernel.org/linux-trace-kernel/170484558617.178953.1590516949390270842.stgit@devnote2 Fixes: 60c8971899f3 ("ftrace: Make DIRECT_CALLS work WITH_ARGS and !WITH_REGS") Cc: stable@xxxxxxxxxxxxxxx Cc: Florent Revest <revest@xxxxxxxxxxxx> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx> Reviewed-by: Mark Rutland <mark.rutland@xxxxxxx> Tested-by: Mark Rutland <mark.rutland@xxxxxxx> [arm64] Acked-by: Jiri Olsa <jolsa@xxxxxxxxxx> Signed-off-by: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- kernel/trace/ftrace.c | 10 ++++++++++ 1 file changed, 10 insertions(+) --- a/kernel/trace/ftrace.c +++ b/kernel/trace/ftrace.c @@ -5325,7 +5325,17 @@ static LIST_HEAD(ftrace_direct_funcs); static int register_ftrace_function_nolock(struct ftrace_ops *ops); +/* + * If there are multiple ftrace_ops, use SAVE_REGS by default, so that direct + * call will be jumped from ftrace_regs_caller. Only if the architecture does + * not support ftrace_regs_caller but direct_call, use SAVE_ARGS so that it + * jumps from ftrace_caller for multiple ftrace_ops. + */ +#ifndef HAVE_DYNAMIC_FTRACE_WITH_REGS #define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_ARGS) +#else +#define MULTI_FLAGS (FTRACE_OPS_FL_DIRECT | FTRACE_OPS_FL_SAVE_REGS) +#endif static int check_direct_multi(struct ftrace_ops *ops) { Patches currently in stable-queue which might be from mhiramat@xxxxxxxxxx are queue-6.7/eventfs-keep-all-directory-links-at-1.patch queue-6.7/eventfs-save-directory-inodes-in-the-eventfs_inode-structure.patch queue-6.7/tracefs-zero-out-the-tracefs_inode-when-allocating-it.patch queue-6.7/eventfs-read-ei-entries-before-ei-children-in-eventfs_iterate.patch queue-6.7/eventfs-remove-lookup-parameter-from-create_dir-file_dentry.patch queue-6.7/eventfs-remove-fsnotify-functions-from-lookup.patch queue-6.7/eventfs-stop-using-dcache_readdir-for-getdents.patch queue-6.7/tracing-probes-fix-to-set-arg-size-and-fmt-after-setting-type-from-btf.patch queue-6.7/tracefs-avoid-using-the-ei-dentry-pointer-unnecessarily.patch queue-6.7/eventfs-initialize-the-tracefs-inode-properly.patch queue-6.7/eventfs-do-ctx-pos-update-for-all-iterations-in-eventfs_iterate.patch queue-6.7/tracefs-dentry-lookup-crapectomy.patch queue-6.7/eventfs-have-the-inodes-all-for-files-and-directories-all-be-the-same.patch queue-6.7/eventfs-use-kcalloc-instead-of-kzalloc.patch queue-6.7/tracing-fix-wasted-memory-in-saved_cmdlines-logic.patch queue-6.7/tracing-synthetic-fix-trace_string-return-value.patch queue-6.7/tracing-probes-fix-to-search-structure-fields-correctly.patch queue-6.7/eventfs-shortcut-eventfs_iterate-by-skipping-entries-already-read.patch queue-6.7/eventfs-do-not-create-dentries-nor-inodes-in-iterate_shared.patch queue-6.7/eventfs-have-eventfs_iterate-stop-immediately-if-ei-is_freed-is-set.patch queue-6.7/eventfs-restructure-eventfs_inode-structure-to-be-more-condensed.patch queue-6.7/eventfs-warn-if-an-eventfs_inode-is-freed-without-is_freed-being-set.patch queue-6.7/eventfs-get-rid-of-dentry-pointers-without-refcounts.patch queue-6.7/tracing-timerlat-move-hrtimer_init-to-timerlat_fd-open.patch queue-6.7/eventfs-remove-unused-d_parent-pointer-field.patch queue-6.7/tracing-probes-fix-to-show-a-parse-error-for-bad-type-for-comm.patch queue-6.7/ftrace-fix-direct_calls-to-use-save_regs-by-default.patch queue-6.7/tracing-trigger-fix-to-return-error-if-failed-to-alloc-snapshot.patch queue-6.7/eventfs-clean-up-dentry-ops-and-add-revalidate-function.patch