[RFC PATCH bpf-next 08/17] bpf: Factor bpf_trampoline_lookup function

Jiri Olsa
2022-08-08T14:08:06Z

Factoring bpf_trampoline_lookup function and adding to new
functions which will be needed in following changes:

struct bpf_trampoline *__bpf_trampoline_lookup(u64 key)
- returns trampoline without locking trampoline_mutex

static struct bpf_trampoline *bpf_trampoline_alloc(void)
- alocates trampoline object

The bpf_trampoline_lookup logic stays the same.

Signed-off-by: Jiri Olsa <jolsa@kernel.org>
---
kernel/bpf/trampoline.c | 45 ++++++++++++++++++++++++++++++-----------
1 file changed, 33 insertions(+), 12 deletions(-)

diff --git a/kernel/bpf/trampoline.c b/kernel/bpf/trampoline.c
index e926692ded85..15801f6c5071 100644
--- a/kernel/bpf/trampoline.c
+++ b/kernel/bpf/trampoline.c
@@ -188,38 +188,59 @@ void bpf_tramp_id_put(struct bpf_tramp_id *id)
kfree(id);
}

-static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
+static struct bpf_trampoline *__bpf_trampoline_lookup(u64 key)
{
struct bpf_trampoline *tr;
struct hlist_head *head;

- mutex_lock(&trampoline_mutex);
head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
hlist_for_each_entry(tr, head, hlist) {
- if (tr->key == key) {
- refcount_inc(&tr->refcnt);
- goto out;
- }
+ if (tr->key == key)
+ return tr;
}
+ return NULL;
+}
+
+static struct bpf_trampoline *bpf_trampoline_alloc(void)
+{
+ struct bpf_trampoline *tr;
+
tr = kzalloc(sizeof(*tr), GFP_KERNEL);
if (!tr)
- goto out;
+ return NULL;
+
+ INIT_HLIST_NODE(&tr->hlist);
+ refcount_set(&tr->refcnt, 1);
+ mutex_init(&tr->mutex);
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL);
if (!tr->fops) {
kfree(tr);
- tr = NULL;
- goto out;
+ return NULL;
}
tr->fops->private = tr;
tr->fops->ops_func = bpf_tramp_ftrace_ops_func;
#endif
+ return tr;
+}
+
+static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
+{
+ struct bpf_trampoline *tr;
+ struct hlist_head *head;

+ mutex_lock(&trampoline_mutex);
+ tr = __bpf_trampoline_lookup(key);
+ if (tr) {
+ refcount_inc(&tr->refcnt);
+ goto out;
+ }
+ tr = bpf_trampoline_alloc();
+ if (!tr)
+ goto out;
tr->key = key;
- INIT_HLIST_NODE(&tr->hlist);
+ head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
hlist_add_head(&tr->hlist, head);
- refcount_set(&tr->refcnt, 1);
- mutex_init(&tr->mutex);
out:
mutex_unlock(&trampoline_mutex);
return tr;
--
2.37.1