Skip to content

Commit 2015ec5

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 aa4bc20 commit 2015ec5

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
@@ -553,6 +553,7 @@ int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr);
553553

554554
int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash);
555555
int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash);
556+
int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock);
556557

557558
void ftrace_stub_direct_tramp(void);
558559

@@ -590,6 +591,11 @@ static inline int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace
590591
return -ENODEV;
591592
}
592593

594+
static inline int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock)
595+
{
596+
return -ENODEV;
597+
}
598+
593599
/*
594600
* This must be implemented by the architecture.
595601
* 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
@@ -6501,6 +6501,74 @@ int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
65016501
return err;
65026502
}
65036503

6504+
int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock)
6505+
{
6506+
struct ftrace_hash *orig_hash = ops->func_hash->filter_hash;
6507+
struct ftrace_func_entry *entry, *tmp;
6508+
static struct ftrace_ops tmp_ops = {
6509+
.func = ftrace_stub,
6510+
.flags = FTRACE_OPS_FL_STUB,
6511+
};
6512+
unsigned long size, i;
6513+
int err;
6514+
6515+
if (!hash_count(hash))
6516+
return -EINVAL;
6517+
if (check_direct_multi(ops))
6518+
return -EINVAL;
6519+
if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6520+
return -EINVAL;
6521+
if (direct_functions == EMPTY_HASH)
6522+
return -EINVAL;
6523+
6524+
if (do_direct_lock)
6525+
mutex_lock(&direct_mutex);
6526+
6527+
/* Enable the tmp_ops to have the same functions as the direct ops */
6528+
ftrace_ops_init(&tmp_ops);
6529+
tmp_ops.func_hash = ops->func_hash;
6530+
6531+
err = register_ftrace_function_nolock(&tmp_ops);
6532+
if (err)
6533+
goto unlock;
6534+
6535+
/*
6536+
* Call __ftrace_hash_update_ipmodify() here, so that we can call
6537+
* ops->ops_func for the ops. This is needed because the above
6538+
* register_ftrace_function_nolock() worked on tmp_ops.
6539+
*/
6540+
err = __ftrace_hash_update_ipmodify(ops, orig_hash, orig_hash, true);
6541+
if (err)
6542+
goto out;
6543+
6544+
/*
6545+
* Now the ftrace_ops_list_func() is called to do the direct callers.
6546+
* We can safely change the direct functions attached to each entry.
6547+
*/
6548+
mutex_lock(&ftrace_lock);
6549+
6550+
size = 1 << hash->size_bits;
6551+
for (i = 0; i < size; i++) {
6552+
hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
6553+
tmp = __ftrace_lookup_ip(direct_functions, entry->ip);
6554+
if (!tmp)
6555+
continue;
6556+
tmp->direct = entry->direct;
6557+
}
6558+
}
6559+
6560+
mutex_unlock(&ftrace_lock);
6561+
6562+
out:
6563+
/* Removing the tmp_ops will add the updated direct callers to the functions */
6564+
unregister_ftrace_function(&tmp_ops);
6565+
6566+
unlock:
6567+
if (do_direct_lock)
6568+
mutex_unlock(&direct_mutex);
6569+
return err;
6570+
}
6571+
65046572
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
65056573

65066574
/**

0 commit comments

Comments
 (0)