Adding modify_ftrace_direct_hash function that modifies
all entries (ip -> direct) provided in hash argument.

The difference to current unregister_ftrace_direct is
- hash argument that allows to modify multiple ip -> direct
  entries at once

This change will allow us to have simple ftrace_ops for all bpf
direct interface users in following changes.

Signed-off-by: Jiri Olsa <jo...@kernel.org>
---
 include/linux/ftrace.h |  6 +++++
 kernel/trace/ftrace.c  | 58 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/include/linux/ftrace.h b/include/linux/ftrace.h
index 7ff6004498c0..8761765d9abc 100644
--- a/include/linux/ftrace.h
+++ b/include/linux/ftrace.h
@@ -528,6 +528,7 @@ int modify_ftrace_direct_nolock(struct ftrace_ops *ops, 
unsigned long addr);
 
 int register_ftrace_direct_hash(struct ftrace_ops *ops, struct ftrace_hash 
*hash);
 int unregister_ftrace_direct_hash(struct ftrace_ops *ops, struct ftrace_hash 
*hash);
+int modify_ftrace_direct_hash(struct ftrace_ops *ops, struct ftrace_hash 
*hash, bool do_direct_lock);
 
 void ftrace_stub_direct_tramp(void);
 
@@ -565,6 +566,11 @@ int unregister_ftrace_direct_hash(struct ftrace_ops *ops, 
struct ftrace_hash *ha
        return -ENODEV;
 }
 
+int modify_ftrace_direct_hash(struct ftrace_ops *ops, struct ftrace_hash 
*hash, bool do_direct_lock)
+{
+       return -ENODEV;
+}
+
 /*
  * This must be implemented by the architecture.
  * It is the way the ftrace direct_ops helper, when called
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index d761237ec70f..755d5550ac44 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -6440,6 +6440,64 @@ int unregister_ftrace_direct_hash(struct ftrace_ops 
*ops, struct ftrace_hash *ha
 }
 EXPORT_SYMBOL_GPL(unregister_ftrace_direct_hash);
 
+int modify_ftrace_direct_hash(struct ftrace_ops *ops, struct ftrace_hash 
*hash, bool do_direct_lock)
+{
+       struct ftrace_func_entry *entry, *tmp;
+       static struct ftrace_ops tmp_ops = {
+               .func           = ftrace_stub,
+               .flags          = FTRACE_OPS_FL_STUB,
+       };
+       unsigned long size, i;
+       int err;
+
+       if (!hash_count(hash))
+               return 0;
+       if (check_direct_multi(ops))
+               return -EINVAL;
+       if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
+               return -EINVAL;
+       if (direct_functions == EMPTY_HASH)
+               return -EINVAL;
+
+       if (do_direct_lock)
+               mutex_lock(&direct_mutex);
+
+       /* Enable the tmp_ops to have the same functions as the direct ops */
+       ftrace_ops_init(&tmp_ops);
+       tmp_ops.func_hash = ops->func_hash;
+
+       err = register_ftrace_function_nolock(&tmp_ops);
+       if (err)
+               goto unlock;
+
+       /*
+        * Now the ftrace_ops_list_func() is called to do the direct callers.
+        * We can safely change the direct functions attached to each entry.
+        */
+       mutex_lock(&ftrace_lock);
+
+       size = 1 << hash->size_bits;
+       for (i = 0; i < size; i++) {
+               hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
+                       tmp = __ftrace_lookup_ip(direct_functions, entry->ip);
+                       if (!tmp)
+                               continue;
+                       tmp->direct = entry->direct;
+               }
+       }
+
+       mutex_unlock(&ftrace_lock);
+
+       /* Removing the tmp_ops will add the updated direct callers to the 
functions */
+       unregister_ftrace_function(&tmp_ops);
+
+unlock:
+       if (do_direct_lock)
+               mutex_unlock(&direct_mutex);
+       return err;
+}
+EXPORT_SYMBOL_GPL(modify_ftrace_direct_hash);
+
 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
 
 /**
-- 
2.50.1


Reply via email to