Skip to content

Commit 05f6b24

Browse files
olsajiriKernel Patches Daemon
authored andcommitted
bpf: Add trampoline ip hash table
Following changes need to lookup trampoline based on its ip address, adding hash table for that. Signed-off-by: Jiri Olsa <jolsa@kernel.org>
1 parent 2f0370f commit 05f6b24

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

include/linux/bpf.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,14 +1295,17 @@ struct bpf_tramp_image {
12951295
};
12961296

12971297
struct bpf_trampoline {
1298-
/* hlist for trampoline_table */
1299-
struct hlist_node hlist;
1298+
/* hlist for trampoline_key_table */
1299+
struct hlist_node hlist_key;
1300+
/* hlist for trampoline_ip_table */
1301+
struct hlist_node hlist_ip;
13001302
struct ftrace_ops *fops;
13011303
/* serializes access to fields of this trampoline */
13021304
struct mutex mutex;
13031305
refcount_t refcnt;
13041306
u32 flags;
13051307
u64 key;
1308+
unsigned long ip;
13061309
struct {
13071310
struct btf_func_model model;
13081311
void *addr;

kernel/bpf/trampoline.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ const struct bpf_prog_ops bpf_extension_prog_ops = {
2424
#define TRAMPOLINE_HASH_BITS 10
2525
#define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
2626

27-
static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
27+
static struct hlist_head trampoline_key_table[TRAMPOLINE_TABLE_SIZE];
28+
static struct hlist_head trampoline_ip_table[TRAMPOLINE_TABLE_SIZE];
2829

29-
/* serializes access to trampoline_table */
30+
/* serializes access to trampoline tables */
3031
static DEFINE_MUTEX(trampoline_mutex);
3132

3233
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
@@ -135,15 +136,15 @@ void bpf_image_ksym_del(struct bpf_ksym *ksym)
135136
PAGE_SIZE, true, ksym->name);
136137
}
137138

138-
static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
139+
static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip)
139140
{
140141
struct bpf_trampoline *tr;
141142
struct hlist_head *head;
142143
int i;
143144

144145
mutex_lock(&trampoline_mutex);
145-
head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
146-
hlist_for_each_entry(tr, head, hlist) {
146+
head = &trampoline_key_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
147+
hlist_for_each_entry(tr, head, hlist_key) {
147148
if (tr->key == key) {
148149
refcount_inc(&tr->refcnt);
149150
goto out;
@@ -164,8 +165,12 @@ static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
164165
#endif
165166

166167
tr->key = key;
167-
INIT_HLIST_NODE(&tr->hlist);
168-
hlist_add_head(&tr->hlist, head);
168+
tr->ip = ftrace_location(ip);
169+
INIT_HLIST_NODE(&tr->hlist_key);
170+
INIT_HLIST_NODE(&tr->hlist_ip);
171+
hlist_add_head(&tr->hlist_key, head);
172+
head = &trampoline_ip_table[hash_64(tr->ip, TRAMPOLINE_HASH_BITS)];
173+
hlist_add_head(&tr->hlist_ip, head);
169174
refcount_set(&tr->refcnt, 1);
170175
mutex_init(&tr->mutex);
171176
for (i = 0; i < BPF_TRAMP_MAX; i++)
@@ -801,7 +806,7 @@ void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
801806
prog->aux->attach_btf_id);
802807

803808
bpf_lsm_find_cgroup_shim(prog, &bpf_func);
804-
tr = bpf_trampoline_lookup(key);
809+
tr = bpf_trampoline_lookup(key, 0);
805810
if (WARN_ON_ONCE(!tr))
806811
return;
807812

@@ -821,7 +826,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
821826
{
822827
struct bpf_trampoline *tr;
823828

824-
tr = bpf_trampoline_lookup(key);
829+
tr = bpf_trampoline_lookup(key, tgt_info->tgt_addr);
825830
if (!tr)
826831
return NULL;
827832

@@ -857,7 +862,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
857862
* fexit progs. The fentry-only trampoline will be freed via
858863
* multiple rcu callbacks.
859864
*/
860-
hlist_del(&tr->hlist);
865+
hlist_del(&tr->hlist_key);
866+
hlist_del(&tr->hlist_ip);
861867
if (tr->fops) {
862868
ftrace_free_filter(tr->fops);
863869
kfree(tr->fops);
@@ -1130,7 +1136,9 @@ static int __init init_trampolines(void)
11301136
int i;
11311137

11321138
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1133-
INIT_HLIST_HEAD(&trampoline_table[i]);
1139+
INIT_HLIST_HEAD(&trampoline_key_table[i]);
1140+
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1141+
INIT_HLIST_HEAD(&trampoline_ip_table[i]);
11341142
return 0;
11351143
}
11361144
late_initcall(init_trampolines);

0 commit comments

Comments
 (0)