Skip to content

Commit 0b36af9

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 929d305 commit 0b36af9

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++)
@@ -850,7 +855,7 @@ void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
850855
prog->aux->attach_btf_id);
851856

852857
bpf_lsm_find_cgroup_shim(prog, &bpf_func);
853-
tr = bpf_trampoline_lookup(key);
858+
tr = bpf_trampoline_lookup(key, 0);
854859
if (WARN_ON_ONCE(!tr))
855860
return;
856861

@@ -870,7 +875,7 @@ struct bpf_trampoline *bpf_trampoline_get(u64 key,
870875
{
871876
struct bpf_trampoline *tr;
872877

873-
tr = bpf_trampoline_lookup(key);
878+
tr = bpf_trampoline_lookup(key, tgt_info->tgt_addr);
874879
if (!tr)
875880
return NULL;
876881

@@ -906,7 +911,8 @@ void bpf_trampoline_put(struct bpf_trampoline *tr)
906911
* fexit progs. The fentry-only trampoline will be freed via
907912
* multiple rcu callbacks.
908913
*/
909-
hlist_del(&tr->hlist);
914+
hlist_del(&tr->hlist_key);
915+
hlist_del(&tr->hlist_ip);
910916
if (tr->fops) {
911917
ftrace_free_filter(tr->fops);
912918
kfree(tr->fops);
@@ -1179,7 +1185,9 @@ static int __init init_trampolines(void)
11791185
int i;
11801186

11811187
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1182-
INIT_HLIST_HEAD(&trampoline_table[i]);
1188+
INIT_HLIST_HEAD(&trampoline_key_table[i]);
1189+
for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1190+
INIT_HLIST_HEAD(&trampoline_ip_table[i]);
11831191
return 0;
11841192
}
11851193
late_initcall(init_trampolines);

0 commit comments

Comments
 (0)