Skip to content

Commit 260673a

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
ftrace: Add update_ftrace_direct_mod function
Adding update_ftrace_direct_mod function that modifies all entries (ip -> direct) provided in hash argument to direct ftrace ops and updates its attachments. The difference to current modify_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 <[email protected]>
1 parent e5e953c commit 260673a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

include/linux/ftrace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr);
544544

545545
int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash);
546546
int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash);
547+
int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock);
547548

548549
void ftrace_stub_direct_tramp(void);
549550

@@ -581,6 +582,11 @@ int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
581582
return -ENODEV;
582583
}
583584

585+
int modify_ftrace_direct_hash(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock)
586+
{
587+
return -ENODEV;
588+
}
589+
584590
/*
585591
* This must be implemented by the architecture.
586592
* It is the way the ftrace direct_ops helper, when called

kernel/trace/ftrace.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6445,6 +6445,74 @@ int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
64456445
return err;
64466446
}
64476447

6448+
int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock)
6449+
{
6450+
struct ftrace_hash *orig_hash = ops->func_hash->filter_hash;
6451+
struct ftrace_func_entry *entry, *tmp;
6452+
static struct ftrace_ops tmp_ops = {
6453+
.func = ftrace_stub,
6454+
.flags = FTRACE_OPS_FL_STUB,
6455+
};
6456+
unsigned long size, i;
6457+
int err;
6458+
6459+
if (!hash_count(hash))
6460+
return 0;
6461+
if (check_direct_multi(ops))
6462+
return -EINVAL;
6463+
if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6464+
return -EINVAL;
6465+
if (direct_functions == EMPTY_HASH)
6466+
return -EINVAL;
6467+
6468+
if (do_direct_lock)
6469+
mutex_lock(&direct_mutex);
6470+
6471+
/* Enable the tmp_ops to have the same functions as the direct ops */
6472+
ftrace_ops_init(&tmp_ops);
6473+
tmp_ops.func_hash = ops->func_hash;
6474+
6475+
err = register_ftrace_function_nolock(&tmp_ops);
6476+
if (err)
6477+
goto unlock;
6478+
6479+
/*
6480+
* Call __ftrace_hash_update_ipmodify() here, so that we can call
6481+
* ops->ops_func for the ops. This is needed because the above
6482+
* register_ftrace_function_nolock() worked on tmp_ops.
6483+
*/
6484+
err = __ftrace_hash_update_ipmodify(ops, orig_hash, orig_hash, true);
6485+
if (err)
6486+
goto out;
6487+
6488+
/*
6489+
* Now the ftrace_ops_list_func() is called to do the direct callers.
6490+
* We can safely change the direct functions attached to each entry.
6491+
*/
6492+
mutex_lock(&ftrace_lock);
6493+
6494+
size = 1 << hash->size_bits;
6495+
for (i = 0; i < size; i++) {
6496+
hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
6497+
tmp = __ftrace_lookup_ip(direct_functions, entry->ip);
6498+
if (!tmp)
6499+
continue;
6500+
tmp->direct = entry->direct;
6501+
}
6502+
}
6503+
6504+
mutex_unlock(&ftrace_lock);
6505+
6506+
out:
6507+
/* Removing the tmp_ops will add the updated direct callers to the functions */
6508+
unregister_ftrace_function(&tmp_ops);
6509+
6510+
unlock:
6511+
if (do_direct_lock)
6512+
mutex_unlock(&direct_mutex);
6513+
return err;
6514+
}
6515+
64486516
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
64496517

64506518
/**

0 commit comments

Comments
 (0)