ftrace/direct: Do not disable when switching direct callers

Currently to switch a set of "multi" direct trampolines from one
trampoline to another, a full shutdown of the current set needs to be
done, followed by an update to what trampoline the direct callers would
call, and then re-enabling the callers. This leaves a time when the
functions will not be calling anything, and events may be missed.

Instead, use a trick to allow all the functions with direct trampolines
attached will always call either the new or old trampoline while the
switch is happening. To do this, first attach a "dummy" callback via
ftrace to all the functions that the current direct trampoline is attached
to. This will cause the functions to call the "list func" instead of the
direct trampoline. The list function will call the direct trampoline
"helper" that will set the function it should call as it returns back to
the ftrace trampoline.

At this moment, the direct caller descriptor can safely update the direct
call trampoline. The list function will pick either the new or old
function (depending on the memory coherency model of the architecture).

Now removing the dummy function from each of the locations of the direct
trampoline caller, will put back the direct call, but now to the new
trampoline.

A better visual is:

[ Changing direct call from my_direct_1 to my_direct_2 ]

  <traced_func>:
     call my_direct_1

 ||||||||||||||||||||
 vvvvvvvvvvvvvvvvvvvv

  <traced_func>:
     call ftrace_caller

  <ftrace_caller>:
    [..]
    call ftrace_ops_list_func

	ftrace_ops_list_func()
	{
		ops->func() -> direct_helper -> set rax to my_direct_1 or my_direct_2
	}

   call rax (to either my_direct_1 or my_direct_2

 ||||||||||||||||||||
 vvvvvvvvvvvvvvvvvvvv

  <traced_func>:
     call my_direct_2

Link: https://lore.kernel.org/all/20211014162819.5c85618b@gandalf.local.home/Acked-by: default avatarJiri Olsa <jolsa@redhat.com>
Signed-off-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
parent 5fae941b
...@@ -5561,8 +5561,12 @@ EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi); ...@@ -5561,8 +5561,12 @@ EXPORT_SYMBOL_GPL(unregister_ftrace_direct_multi);
*/ */
int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
{ {
struct ftrace_hash *hash = ops->func_hash->filter_hash; struct ftrace_hash *hash;
struct ftrace_func_entry *entry, *iter; struct ftrace_func_entry *entry, *iter;
static struct ftrace_ops tmp_ops = {
.func = ftrace_stub,
.flags = FTRACE_OPS_FL_STUB,
};
int i, size; int i, size;
int err; int err;
...@@ -5572,21 +5576,22 @@ int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) ...@@ -5572,21 +5576,22 @@ int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
return -EINVAL; return -EINVAL;
mutex_lock(&direct_mutex); mutex_lock(&direct_mutex);
mutex_lock(&ftrace_lock);
/* 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(&tmp_ops);
if (err)
goto out_direct;
/* /*
* Shutdown the ops, change 'direct' pointer for each * Now the ftrace_ops_list_func() is called to do the direct callers.
* ops entry in direct_functions hash and startup the * We can safely change the direct functions attached to each entry.
* ops back again.
*
* Note there is no callback called for @ops object after
* this ftrace_shutdown call until ftrace_startup is called
* later on.
*/ */
err = ftrace_shutdown(ops, 0); mutex_lock(&ftrace_lock);
if (err)
goto out_unlock;
hash = ops->func_hash->filter_hash;
size = 1 << hash->size_bits; size = 1 << hash->size_bits;
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
hlist_for_each_entry(iter, &hash->buckets[i], hlist) { hlist_for_each_entry(iter, &hash->buckets[i], hlist) {
...@@ -5597,10 +5602,11 @@ int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr) ...@@ -5597,10 +5602,11 @@ int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
} }
} }
err = ftrace_startup(ops, 0); /* Removing the tmp_ops will add the updated direct callers to the functions */
unregister_ftrace_function(&tmp_ops);
out_unlock:
mutex_unlock(&ftrace_lock); mutex_unlock(&ftrace_lock);
out_direct:
mutex_unlock(&direct_mutex); mutex_unlock(&direct_mutex);
return err; return err;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment