Skip to content

Commit b56ddea

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 <[email protected]>
1 parent 2ebcb25 commit b56ddea

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
@@ -1307,14 +1307,17 @@ struct bpf_tramp_image {
13071307
};
13081308

13091309
struct bpf_trampoline {
1310-
/* hlist for trampoline_table */
1311-
struct hlist_node hlist;
1310+
/* hlist for trampoline_key_table */
1311+
struct hlist_node hlist_key;
1312+
/* hlist for trampoline_ip_table */
1313+
struct hlist_node hlist_ip;
13121314
struct ftrace_ops *fops;
13131315
/* serializes access to fields of this trampoline */
13141316
struct mutex mutex;
13151317
refcount_t refcnt;
13161318
u32 flags;
13171319
u64 key;
1320+
unsigned long ip;
13181321
struct {
13191322
struct btf_func_model model;
13201323
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++)
@@ -846,7 +851,7 @@ void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
846851
prog->aux->attach_btf_id);
847852

848853
bpf_lsm_find_cgroup_shim(prog, &bpf_func);
849-
tr = bpf_trampoline_lookup(key);
854+
tr = bpf_trampoline_lookup(key, 0);
850855
if (WARN_ON_ONCE(!tr))
851856
return;
852857

@@ -866,7 +871,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
866871
{
867872
struct bpf_trampoline *tr;
868873

869-
tr = bpf_trampoline_lookup(key);
874+
tr = bpf_trampoline_lookup(key, tgt_info->tgt_addr);
870875
if (!tr)
871876
return NULL;
872877

@@ -902,7 +907,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
902907
* fexit progs. The fentry-only trampoline will be freed via
903908
* multiple rcu callbacks.
904909
*/
905-
hlist_del(&tr->hlist);
910+
hlist_del(&tr->hlist_key);
911+
hlist_del(&tr->hlist_ip);
906912
if (tr->fops) {
907913
ftrace_free_filter(tr->fops);
908914
kfree(tr->fops);
@@ -1175,7 +1181,9 @@ static int __init init_trampolines(void)
11751181
int i;
11761182

11771183
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1178-
INIT_HLIST_HEAD(&trampoline_table[i]);
1184+
INIT_HLIST_HEAD(&trampoline_key_table[i]);
1185+
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1186+
INIT_HLIST_HEAD(&trampoline_ip_table[i]);
11791187
return 0;
11801188
}
11811189
late_initcall(init_trampolines);

0 commit comments

Comments
 (0)