1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
3 #include <linux/hash.h>
5 #include <linux/filter.h>
7 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
8 #define TRAMPOLINE_HASH_BITS 10
9 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
11 static struct hlist_head trampoline_table
[TRAMPOLINE_TABLE_SIZE
];
13 /* serializes access to trampoline_table */
14 static DEFINE_MUTEX(trampoline_mutex
);
16 struct bpf_trampoline
*bpf_trampoline_lookup(u64 key
)
18 struct bpf_trampoline
*tr
;
19 struct hlist_head
*head
;
23 mutex_lock(&trampoline_mutex
);
24 head
= &trampoline_table
[hash_64(key
, TRAMPOLINE_HASH_BITS
)];
25 hlist_for_each_entry(tr
, head
, hlist
) {
27 refcount_inc(&tr
->refcnt
);
31 tr
= kzalloc(sizeof(*tr
), GFP_KERNEL
);
35 /* is_root was checked earlier. No need for bpf_jit_charge_modmem() */
36 image
= bpf_jit_alloc_exec(PAGE_SIZE
);
44 INIT_HLIST_NODE(&tr
->hlist
);
45 hlist_add_head(&tr
->hlist
, head
);
46 refcount_set(&tr
->refcnt
, 1);
47 mutex_init(&tr
->mutex
);
48 for (i
= 0; i
< BPF_TRAMP_MAX
; i
++)
49 INIT_HLIST_HEAD(&tr
->progs_hlist
[i
]);
51 set_vm_flush_reset_perms(image
);
52 /* Keep image as writeable. The alternative is to keep flipping ro/rw
53 * everytime new program is attached or detached.
55 set_memory_x((long)image
, 1);
58 mutex_unlock(&trampoline_mutex
);
62 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
63 * bytes on x86. Pick a number to fit into PAGE_SIZE / 2
65 #define BPF_MAX_TRAMP_PROGS 40
67 static int bpf_trampoline_update(struct bpf_trampoline
*tr
)
69 void *old_image
= tr
->image
+ ((tr
->selector
+ 1) & 1) * PAGE_SIZE
/2;
70 void *new_image
= tr
->image
+ (tr
->selector
& 1) * PAGE_SIZE
/2;
71 struct bpf_prog
*progs_to_run
[BPF_MAX_TRAMP_PROGS
];
72 int fentry_cnt
= tr
->progs_cnt
[BPF_TRAMP_FENTRY
];
73 int fexit_cnt
= tr
->progs_cnt
[BPF_TRAMP_FEXIT
];
74 struct bpf_prog
**progs
, **fentry
, **fexit
;
75 u32 flags
= BPF_TRAMP_F_RESTORE_REGS
;
76 struct bpf_prog_aux
*aux
;
79 if (fentry_cnt
+ fexit_cnt
== 0) {
80 err
= bpf_arch_text_poke(tr
->func
.addr
, BPF_MOD_CALL
,
86 /* populate fentry progs */
87 fentry
= progs
= progs_to_run
;
88 hlist_for_each_entry(aux
, &tr
->progs_hlist
[BPF_TRAMP_FENTRY
], tramp_hlist
)
91 /* populate fexit progs */
93 hlist_for_each_entry(aux
, &tr
->progs_hlist
[BPF_TRAMP_FEXIT
], tramp_hlist
)
97 flags
= BPF_TRAMP_F_CALL_ORIG
| BPF_TRAMP_F_SKIP_FRAME
;
99 err
= arch_prepare_bpf_trampoline(new_image
, &tr
->func
.model
, flags
,
107 /* progs already running at this address */
108 err
= bpf_arch_text_poke(tr
->func
.addr
, BPF_MOD_CALL
,
109 old_image
, new_image
);
111 /* first time registering */
112 err
= bpf_arch_text_poke(tr
->func
.addr
, BPF_MOD_CALL
, NULL
,
121 static enum bpf_tramp_prog_type
bpf_attach_type_to_tramp(enum bpf_attach_type t
)
124 case BPF_TRACE_FENTRY
:
125 return BPF_TRAMP_FENTRY
;
127 return BPF_TRAMP_FEXIT
;
131 int bpf_trampoline_link_prog(struct bpf_prog
*prog
)
133 enum bpf_tramp_prog_type kind
;
134 struct bpf_trampoline
*tr
;
137 tr
= prog
->aux
->trampoline
;
138 kind
= bpf_attach_type_to_tramp(prog
->expected_attach_type
);
139 mutex_lock(&tr
->mutex
);
140 if (tr
->progs_cnt
[BPF_TRAMP_FENTRY
] + tr
->progs_cnt
[BPF_TRAMP_FEXIT
]
141 >= BPF_MAX_TRAMP_PROGS
) {
145 if (!hlist_unhashed(&prog
->aux
->tramp_hlist
)) {
146 /* prog already linked */
150 hlist_add_head(&prog
->aux
->tramp_hlist
, &tr
->progs_hlist
[kind
]);
151 tr
->progs_cnt
[kind
]++;
152 err
= bpf_trampoline_update(prog
->aux
->trampoline
);
154 hlist_del(&prog
->aux
->tramp_hlist
);
155 tr
->progs_cnt
[kind
]--;
158 mutex_unlock(&tr
->mutex
);
162 /* bpf_trampoline_unlink_prog() should never fail. */
163 int bpf_trampoline_unlink_prog(struct bpf_prog
*prog
)
165 enum bpf_tramp_prog_type kind
;
166 struct bpf_trampoline
*tr
;
169 tr
= prog
->aux
->trampoline
;
170 kind
= bpf_attach_type_to_tramp(prog
->expected_attach_type
);
171 mutex_lock(&tr
->mutex
);
172 hlist_del(&prog
->aux
->tramp_hlist
);
173 tr
->progs_cnt
[kind
]--;
174 err
= bpf_trampoline_update(prog
->aux
->trampoline
);
175 mutex_unlock(&tr
->mutex
);
179 void bpf_trampoline_put(struct bpf_trampoline
*tr
)
183 mutex_lock(&trampoline_mutex
);
184 if (!refcount_dec_and_test(&tr
->refcnt
))
186 WARN_ON_ONCE(mutex_is_locked(&tr
->mutex
));
187 if (WARN_ON_ONCE(!hlist_empty(&tr
->progs_hlist
[BPF_TRAMP_FENTRY
])))
189 if (WARN_ON_ONCE(!hlist_empty(&tr
->progs_hlist
[BPF_TRAMP_FEXIT
])))
191 bpf_jit_free_exec(tr
->image
);
192 hlist_del(&tr
->hlist
);
195 mutex_unlock(&trampoline_mutex
);
198 /* The logic is similar to BPF_PROG_RUN, but with explicit rcu and preempt that
199 * are needed for trampoline. The macro is split into
200 * call _bpf_prog_enter
201 * call prog->bpf_func
202 * call __bpf_prog_exit
204 u64 notrace
__bpf_prog_enter(void)
210 if (static_branch_unlikely(&bpf_stats_enabled_key
))
211 start
= sched_clock();
215 void notrace
__bpf_prog_exit(struct bpf_prog
*prog
, u64 start
)
217 struct bpf_prog_stats
*stats
;
219 if (static_branch_unlikely(&bpf_stats_enabled_key
) &&
220 /* static_key could be enabled in __bpf_prog_enter
221 * and disabled in __bpf_prog_exit.
223 * Hence check that 'start' is not zero.
226 stats
= this_cpu_ptr(prog
->aux
->stats
);
227 u64_stats_update_begin(&stats
->syncp
);
229 stats
->nsecs
+= sched_clock() - start
;
230 u64_stats_update_end(&stats
->syncp
);
237 arch_prepare_bpf_trampoline(void *image
, struct btf_func_model
*m
, u32 flags
,
238 struct bpf_prog
**fentry_progs
, int fentry_cnt
,
239 struct bpf_prog
**fexit_progs
, int fexit_cnt
,
245 static int __init
init_trampolines(void)
249 for (i
= 0; i
< TRAMPOLINE_TABLE_SIZE
; i
++)
250 INIT_HLIST_HEAD(&trampoline_table
[i
]);
253 late_initcall(init_trampolines
);