Skip to content

Commit 2f0370f

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 <jolsa@kernel.org>
1 parent 472e2b4 commit 2f0370f

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

553553
int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash);
554554
int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash);
555+
int update_ftrace_direct_mod(struct ftrace_ops *ops, struct ftrace_hash *hash, bool do_direct_lock);
555556

556557
void ftrace_stub_direct_tramp(void);
557558

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

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

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

64916559
/**

0 commit comments

Comments
 (0)