Skip to content

Commit c2185e3

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 cb27c5a commit c2185e3

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
@@ -551,6 +551,7 @@ int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
551551
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);
554+
int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash);
554555

555556
void ftrace_stub_direct_tramp(void);
556557

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

587+
static inline int update_ftrace_direct_del(struct ftrace_ops *ops, struct ftrace_hash *hash)
588+
{
589+
return -ENODEV;
590+
}
591+
586592
/*
587593
* This must be implemented by the architecture.
588594
* 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
@@ -6376,6 +6376,116 @@ int update_ftrace_direct_add(struct ftrace_ops *ops, struct ftrace_hash *hash)
63766376
return err;
63776377
}
63786378

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

63816491
/**

0 commit comments

Comments
 (0)