Skip to content

Commit 4f7b3fd

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
ftrace: Add update_ftrace_direct_del function
Adding update_ftrace_direct_del function that removes all entries (ip -> addr) provided in hash argument to direct ftrace ops and updates its attachments. The difference to current unregister_ftrace_direct is - hash argument that allows to unregister multiple ip -> direct entries at once - we can call update_ftrace_direct_del multiple times on the same ftrace_ops object, becase we do not need to unregister all entries at once, we can do it gradualy with the help of ftrace_update_ops function 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 d62b77e commit 4f7b3fd

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-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(struct ftrace_ops *ops, unsigned long addr);
552552
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);
555+
int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash);
555556

556557
void ftrace_stub_direct_tramp(void);
557558

@@ -584,6 +585,11 @@ static inline int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace
584585
return -ENODEV;
585586
}
586587

588+
static inline int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
589+
{
590+
return -ENODEV;
591+
}
592+
587593
/*
588594
* This must be implemented by the architecture.
589595
* It is the way the ftrace direct_ops helper, when called

kernel/trace/ftrace.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6391,6 +6391,116 @@ int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash)
63916391
return err;
63926392
}
63936393

6394+
/**
6395+
* hash_sub - substracts @b from @a and returns the result
6396+
* @a: struct ftrace_hash object
6397+
* @b: struct ftrace_hash object
6398+
*
6399+
* Returns struct ftrace_hash object on success, NULL on error.
6400+
*/
6401+
static struct ftrace_hash *hash_sub(struct ftrace_hash *a, struct ftrace_hash *b)
6402+
{
6403+
struct ftrace_func_entry *entry, *del;
6404+
struct ftrace_hash *sub;
6405+
int size, i;
6406+
6407+
sub = alloc_and_copy_ftrace_hash(a->size_bits, a);
6408+
if (!sub)
6409+
goto error;
6410+
6411+
size = 1 << b->size_bits;
6412+
for (i = 0; i < size; i++) {
6413+
hlist_for_each_entry(entry, &b->buckets[i], hlist) {
6414+
del = __ftrace_lookup_ip(sub, entry->ip);
6415+
if (WARN_ON_ONCE(!del))
6416+
goto error;
6417+
remove_hash_entry(sub, del);
6418+
kfree(del);
6419+
}
6420+
}
6421+
return sub;
6422+
6423+
error:
6424+
free_ftrace_hash(sub);
6425+
return NULL;
6426+
}
6427+
6428+
int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
6429+
{
6430+
struct ftrace_hash *old_direct_functions = NULL, *new_direct_functions = NULL;
6431+
struct ftrace_hash *old_filter_hash = NULL, *new_filter_hash = NULL;
6432+
struct ftrace_func_entry *del, *entry;
6433+
unsigned long size, i;
6434+
int err = -EINVAL;
6435+
6436+
if (!hash_count(hash))
6437+
return -EINVAL;
6438+
if (check_direct_multi(ops))
6439+
return -EINVAL;
6440+
if (!(ops->flags & FTRACE_OPS_FL_ENABLED))
6441+
return -EINVAL;
6442+
if (direct_functions == EMPTY_HASH)
6443+
return -EINVAL;
6444+
6445+
mutex_lock(&direct_mutex);
6446+
6447+
old_filter_hash = ops->func_hash ? ops->func_hash->filter_hash : NULL;
6448+
old_direct_functions = direct_functions;
6449+
6450+
/* Make sure requested entries are already registered. */
6451+
size = 1 << hash->size_bits;
6452+
for (i = 0; i < size; i++) {
6453+
hlist_for_each_entry(entry, &hash->buckets[i], hlist) {
6454+
del = __ftrace_lookup_ip(direct_functions, entry->ip);
6455+
if (!del || del->direct != entry->direct)
6456+
goto out_unlock;
6457+
}
6458+
}
6459+
6460+
err = -ENOMEM;
6461+
new_filter_hash = hash_sub(old_filter_hash, hash);
6462+
if (!new_filter_hash)
6463+
goto out_unlock;
6464+
6465+
new_direct_functions = hash_sub(old_direct_functions, hash);
6466+
if (!new_direct_functions)
6467+
goto out_unlock;
6468+
6469+
/* If there's nothing left, we need to unregister the ops. */
6470+
if (ftrace_hash_empty(new_filter_hash)) {
6471+
err = unregister_ftrace_function(ops);
6472+
if (!err) {
6473+
/* cleanup for possible another register call */
6474+
ops->func = NULL;
6475+
ops->trampoline = 0;
6476+
ftrace_free_filter(ops);
6477+
ops->func_hash->filter_hash = NULL;
6478+
}
6479+
} else {
6480+
err = ftrace_update_ops(ops, new_filter_hash, EMPTY_HASH);
6481+
/*
6482+
* new_filter_hash is dup-ed, so we need to release it anyway,
6483+
* old_filter_hash either stays on error or is released already
6484+
*/
6485+
}
6486+
6487+
if (err) {
6488+
/* reset direct_functions and free the new one */
6489+
old_direct_functions = new_direct_functions;
6490+
} else {
6491+
rcu_assign_pointer(direct_functions, new_direct_functions);
6492+
}
6493+
6494+
out_unlock:
6495+
mutex_unlock(&direct_mutex);
6496+
6497+
if (old_direct_functions && old_direct_functions != EMPTY_HASH)
6498+
call_rcu_tasks(&old_direct_functions->rcu, register_ftrace_direct_cb);
6499+
free_ftrace_hash(new_filter_hash);
6500+
6501+
return err;
6502+
}
6503+
63946504
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
63956505

63966506
/**

0 commit comments

Comments
 (0)