Re: [PATCH 03/10] ftrace: Add register_ftrace_direct()

From: Steven Rostedt
Date: Thu Nov 14 2019 - 14:06:08 EST


On Thu, 14 Nov 2019 13:48:27 -0500
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> The main reason, is that then we need to add another arch specific
> change, where as, the solution I suggested doesn't need anything new.
> The less arch specific code we need the better.

Here's the change, to see how easy it is (compiled tested only):

/* still needs comments */

diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 82ef8d60a42b..4231571db30f 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -5160,6 +5160,59 @@ int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
return ret;
}
EXPORT_SYMBOL_GPL(unregister_ftrace_direct);
+
+static struct ftrace_ops stub_ops = {
+ .func = ftrace_stub,
+};
+
+int modify_ftrace_direct(unsigned long ip,
+ unsigned long old_addr, unsigned long new_addr)
+{
+ struct ftrace_func_entry *entry;
+ struct dyn_ftrace *rec;
+ int ret = -ENODEV;
+
+ mutex_lock(&direct_mutex);
+ entry = __ftrace_lookup_ip(direct_functions, ip);
+ if (!entry) {
+ /* OK if it is off by a little */
+ rec = lookup_rec(ip, ip);
+ if (!rec || rec->ip == ip)
+ goto out_unlock;
+
+ entry = __ftrace_lookup_ip(direct_functions, rec->ip);
+ if (!entry)
+ goto out_unlock;
+
+ WARN_ON(!(rec->flags & FTRACE_FL_DIRECT));
+ }
+
+ ret = -EINVAL;
+ if (entry->direct != old_addr)
+ goto out_unlock;
+
+ ret = ftrace_set_filter_ip(&stub_ops, ip, 0, 0);
+ if (ret)
+ goto out_unlock;
+
+ ret = register_ftrace_function(&stub_ops);
+ if (ret) {
+ ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
+ goto out_unlock;
+ }
+
+ entry->direct = new_addr;
+
+ unregister_ftrace_function(&stub_ops);
+ ftrace_set_filter_ip(&stub_ops, ip, 1, 0);
+
+ ret = 0;
+
+ out_unlock:
+ mutex_unlock(&direct_mutex);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(modify_ftrace_direct);
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */

/**


-- Steve