blob: e98de5e73ba59f756480cc5f962849be1859751b [file] [log] [blame]
Alexei Starovoitovfec56f52019-11-14 10:57:04 -08001// SPDX-License-Identifier: GPL-2.0-only
2/* Copyright (c) 2019 Facebook */
3#include <linux/hash.h>
4#include <linux/bpf.h>
5#include <linux/filter.h>
Alexei Starovoitovb91e0142019-12-08 16:01:13 -08006#include <linux/ftrace.h>
Jiri Olsae9b4e602020-01-23 17:15:07 +01007#include <linux/rbtree_latch.h>
Jiri Olsaa108f7d2020-03-12 20:56:05 +01008#include <linux/perf_event.h>
KP Singh9e4e01d2020-03-29 01:43:52 +01009#include <linux/btf.h>
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -070010#include <linux/rcupdate_trace.h>
11#include <linux/rcupdate_wait.h>
Jiri Olsa861de022021-03-26 11:59:00 +010012#include <linux/module.h>
Song Liu856c02d2021-09-10 11:33:51 -070013#include <linux/static_call.h>
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080014
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -080015/* dummy _ops. The verifier will operate on target program's ops. */
16const struct bpf_verifier_ops bpf_extension_verifier_ops = {
17};
18const struct bpf_prog_ops bpf_extension_prog_ops = {
19};
20
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080021/* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
22#define TRAMPOLINE_HASH_BITS 10
23#define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
24
25static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
26
Jiri Olsa7ac88eb2020-03-12 20:56:07 +010027/* serializes access to trampoline_table */
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080028static DEFINE_MUTEX(trampoline_mutex);
29
Jiri Olsa7ac88eb2020-03-12 20:56:07 +010030void *bpf_jit_alloc_exec_page(void)
Björn Töpel98e86272019-12-13 18:51:07 +010031{
32 void *image;
33
34 image = bpf_jit_alloc_exec(PAGE_SIZE);
35 if (!image)
36 return NULL;
37
38 set_vm_flush_reset_perms(image);
39 /* Keep image as writeable. The alternative is to keep flipping ro/rw
40 * everytime new program is attached or detached.
41 */
42 set_memory_x((long)image, 1);
43 return image;
44}
45
Jiri Olsaa108f7d2020-03-12 20:56:05 +010046void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
47{
48 ksym->start = (unsigned long) data;
Jiri Olsa7ac88eb2020-03-12 20:56:07 +010049 ksym->end = ksym->start + PAGE_SIZE;
Jiri Olsaa108f7d2020-03-12 20:56:05 +010050 bpf_ksym_add(ksym);
51 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
Jiri Olsa7ac88eb2020-03-12 20:56:07 +010052 PAGE_SIZE, false, ksym->name);
Jiri Olsaa108f7d2020-03-12 20:56:05 +010053}
54
55void bpf_image_ksym_del(struct bpf_ksym *ksym)
56{
57 bpf_ksym_del(ksym);
58 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
Jiri Olsa7ac88eb2020-03-12 20:56:07 +010059 PAGE_SIZE, true, ksym->name);
Jiri Olsaa108f7d2020-03-12 20:56:05 +010060}
61
Toke Høiland-Jørgensenf7b12b62020-09-25 23:25:02 +020062static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080063{
64 struct bpf_trampoline *tr;
65 struct hlist_head *head;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080066 int i;
67
68 mutex_lock(&trampoline_mutex);
69 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
70 hlist_for_each_entry(tr, head, hlist) {
71 if (tr->key == key) {
72 refcount_inc(&tr->refcnt);
73 goto out;
74 }
75 }
76 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
77 if (!tr)
78 goto out;
79
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080080 tr->key = key;
81 INIT_HLIST_NODE(&tr->hlist);
82 hlist_add_head(&tr->hlist, head);
83 refcount_set(&tr->refcnt, 1);
84 mutex_init(&tr->mutex);
85 for (i = 0; i < BPF_TRAMP_MAX; i++)
86 INIT_HLIST_HEAD(&tr->progs_hlist[i]);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -080087out:
88 mutex_unlock(&trampoline_mutex);
89 return tr;
90}
91
Jiri Olsa861de022021-03-26 11:59:00 +010092static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
93{
94 struct module *mod;
95 int err = 0;
96
97 preempt_disable();
98 mod = __module_text_address((unsigned long) tr->func.addr);
99 if (mod && !try_module_get(mod))
100 err = -ENOENT;
101 preempt_enable();
102 tr->mod = mod;
103 return err;
104}
105
106static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
107{
108 module_put(tr->mod);
109 tr->mod = NULL;
110}
111
Alexei Starovoitovb91e0142019-12-08 16:01:13 -0800112static int is_ftrace_location(void *ip)
113{
114 long addr;
115
116 addr = ftrace_location((long)ip);
117 if (!addr)
118 return 0;
119 if (WARN_ON_ONCE(addr != (long)ip))
120 return -EFAULT;
121 return 1;
122}
123
124static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
125{
126 void *ip = tr->func.addr;
127 int ret;
128
129 if (tr->func.ftrace_managed)
130 ret = unregister_ftrace_direct((long)ip, (long)old_addr);
131 else
132 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
Jiri Olsa861de022021-03-26 11:59:00 +0100133
134 if (!ret)
135 bpf_trampoline_module_put(tr);
Alexei Starovoitovb91e0142019-12-08 16:01:13 -0800136 return ret;
137}
138
139static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr)
140{
141 void *ip = tr->func.addr;
142 int ret;
143
144 if (tr->func.ftrace_managed)
145 ret = modify_ftrace_direct((long)ip, (long)old_addr, (long)new_addr);
146 else
147 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
148 return ret;
149}
150
151/* first time registering */
152static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
153{
154 void *ip = tr->func.addr;
155 int ret;
156
157 ret = is_ftrace_location(ip);
158 if (ret < 0)
159 return ret;
160 tr->func.ftrace_managed = ret;
161
Jiri Olsa861de022021-03-26 11:59:00 +0100162 if (bpf_trampoline_module_get(tr))
163 return -ENOENT;
164
Alexei Starovoitovb91e0142019-12-08 16:01:13 -0800165 if (tr->func.ftrace_managed)
166 ret = register_ftrace_direct((long)ip, (long)new_addr);
167 else
168 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
Jiri Olsa861de022021-03-26 11:59:00 +0100169
170 if (ret)
171 bpf_trampoline_module_put(tr);
Alexei Starovoitovb91e0142019-12-08 16:01:13 -0800172 return ret;
173}
174
KP Singh88fd9e52020-03-04 20:18:47 +0100175static struct bpf_tramp_progs *
Jiri Olsa1e373922021-07-14 11:43:54 +0200176bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)
KP Singh88fd9e52020-03-04 20:18:47 +0100177{
178 const struct bpf_prog_aux *aux;
179 struct bpf_tramp_progs *tprogs;
180 struct bpf_prog **progs;
181 int kind;
182
183 *total = 0;
184 tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
185 if (!tprogs)
186 return ERR_PTR(-ENOMEM);
187
188 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
189 tprogs[kind].nr_progs = tr->progs_cnt[kind];
190 *total += tr->progs_cnt[kind];
191 progs = tprogs[kind].progs;
192
Jiri Olsa1e373922021-07-14 11:43:54 +0200193 hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist) {
194 *ip_arg |= aux->prog->call_get_func_ip;
KP Singh88fd9e52020-03-04 20:18:47 +0100195 *progs++ = aux->prog;
Jiri Olsa1e373922021-07-14 11:43:54 +0200196 }
KP Singh88fd9e52020-03-04 20:18:47 +0100197 }
198 return tprogs;
199}
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800200
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700201static void __bpf_tramp_image_put_deferred(struct work_struct *work)
202{
203 struct bpf_tramp_image *im;
204
205 im = container_of(work, struct bpf_tramp_image, work);
206 bpf_image_ksym_del(&im->ksym);
207 bpf_jit_free_exec(im->image);
208 bpf_jit_uncharge_modmem(1);
209 percpu_ref_exit(&im->pcref);
210 kfree_rcu(im, rcu);
211}
212
213/* callback, fexit step 3 or fentry step 2 */
214static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu)
215{
216 struct bpf_tramp_image *im;
217
218 im = container_of(rcu, struct bpf_tramp_image, rcu);
219 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred);
220 schedule_work(&im->work);
221}
222
223/* callback, fexit step 2. Called after percpu_ref_kill confirms. */
224static void __bpf_tramp_image_release(struct percpu_ref *pcref)
225{
226 struct bpf_tramp_image *im;
227
228 im = container_of(pcref, struct bpf_tramp_image, pcref);
229 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
230}
231
232/* callback, fexit or fentry step 1 */
233static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
234{
235 struct bpf_tramp_image *im;
236
237 im = container_of(rcu, struct bpf_tramp_image, rcu);
238 if (im->ip_after_call)
239 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
240 percpu_ref_kill(&im->pcref);
241 else
242 /* the case of fentry trampoline */
243 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
244}
245
246static void bpf_tramp_image_put(struct bpf_tramp_image *im)
247{
248 /* The trampoline image that calls original function is using:
249 * rcu_read_lock_trace to protect sleepable bpf progs
250 * rcu_read_lock to protect normal bpf progs
251 * percpu_ref to protect trampoline itself
252 * rcu tasks to protect trampoline asm not covered by percpu_ref
253 * (which are few asm insns before __bpf_tramp_enter and
254 * after __bpf_tramp_exit)
255 *
256 * The trampoline is unreachable before bpf_tramp_image_put().
257 *
258 * First, patch the trampoline to avoid calling into fexit progs.
259 * The progs will be freed even if the original function is still
260 * executing or sleeping.
261 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
262 * first few asm instructions to execute and call into
263 * __bpf_tramp_enter->percpu_ref_get.
264 * Then use percpu_ref_kill to wait for the trampoline and the original
265 * function to finish.
266 * Then use call_rcu_tasks() to make sure few asm insns in
267 * the trampoline epilogue are done as well.
268 *
269 * In !PREEMPT case the task that got interrupted in the first asm
270 * insns won't go through an RCU quiescent state which the
271 * percpu_ref_kill will be waiting for. Hence the first
272 * call_rcu_tasks() is not necessary.
273 */
274 if (im->ip_after_call) {
275 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP,
276 NULL, im->ip_epilogue);
277 WARN_ON(err);
278 if (IS_ENABLED(CONFIG_PREEMPTION))
279 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
280 else
281 percpu_ref_kill(&im->pcref);
282 return;
283 }
284
285 /* The trampoline without fexit and fmod_ret progs doesn't call original
286 * function and doesn't use percpu_ref.
287 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
288 * Then use call_rcu_tasks() to wait for the rest of trampoline asm
289 * and normal progs.
290 */
291 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
292}
293
294static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
295{
296 struct bpf_tramp_image *im;
297 struct bpf_ksym *ksym;
298 void *image;
299 int err = -ENOMEM;
300
301 im = kzalloc(sizeof(*im), GFP_KERNEL);
302 if (!im)
303 goto out;
304
305 err = bpf_jit_charge_modmem(1);
306 if (err)
307 goto out_free_im;
308
309 err = -ENOMEM;
310 im->image = image = bpf_jit_alloc_exec_page();
311 if (!image)
312 goto out_uncharge;
313
314 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
315 if (err)
316 goto out_free_image;
317
318 ksym = &im->ksym;
319 INIT_LIST_HEAD_RCU(&ksym->lnode);
320 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx);
321 bpf_image_ksym_add(image, ksym);
322 return im;
323
324out_free_image:
325 bpf_jit_free_exec(im->image);
326out_uncharge:
327 bpf_jit_uncharge_modmem(1);
328out_free_im:
329 kfree(im);
330out:
331 return ERR_PTR(err);
332}
333
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800334static int bpf_trampoline_update(struct bpf_trampoline *tr)
335{
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700336 struct bpf_tramp_image *im;
KP Singh88fd9e52020-03-04 20:18:47 +0100337 struct bpf_tramp_progs *tprogs;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800338 u32 flags = BPF_TRAMP_F_RESTORE_REGS;
Jiri Olsa1e373922021-07-14 11:43:54 +0200339 bool ip_arg = false;
KP Singh88fd9e52020-03-04 20:18:47 +0100340 int err, total;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800341
Jiri Olsa1e373922021-07-14 11:43:54 +0200342 tprogs = bpf_trampoline_get_progs(tr, &total, &ip_arg);
KP Singh88fd9e52020-03-04 20:18:47 +0100343 if (IS_ERR(tprogs))
344 return PTR_ERR(tprogs);
345
346 if (total == 0) {
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700347 err = unregister_fentry(tr, tr->cur_image->image);
348 bpf_tramp_image_put(tr->cur_image);
349 tr->cur_image = NULL;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800350 tr->selector = 0;
351 goto out;
352 }
353
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700354 im = bpf_tramp_image_alloc(tr->key, tr->selector);
355 if (IS_ERR(im)) {
356 err = PTR_ERR(im);
357 goto out;
358 }
359
KP Singhae240822020-03-04 20:18:49 +0100360 if (tprogs[BPF_TRAMP_FEXIT].nr_progs ||
361 tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800362 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
363
Jiri Olsa1e373922021-07-14 11:43:54 +0200364 if (ip_arg)
365 flags |= BPF_TRAMP_F_IP_ARG;
366
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700367 err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
KP Singh88fd9e52020-03-04 20:18:47 +0100368 &tr->func.model, flags, tprogs,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800369 tr->func.addr);
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800370 if (err < 0)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800371 goto out;
372
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700373 WARN_ON(tr->cur_image && tr->selector == 0);
374 WARN_ON(!tr->cur_image && tr->selector);
375 if (tr->cur_image)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800376 /* progs already running at this address */
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700377 err = modify_fentry(tr, tr->cur_image->image, im->image);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800378 else
379 /* first time registering */
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700380 err = register_fentry(tr, im->image);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800381 if (err)
382 goto out;
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700383 if (tr->cur_image)
384 bpf_tramp_image_put(tr->cur_image);
385 tr->cur_image = im;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800386 tr->selector++;
387out:
KP Singh88fd9e52020-03-04 20:18:47 +0100388 kfree(tprogs);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800389 return err;
390}
391
KP Singh9e4e01d2020-03-29 01:43:52 +0100392static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800393{
KP Singh9e4e01d2020-03-29 01:43:52 +0100394 switch (prog->expected_attach_type) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800395 case BPF_TRACE_FENTRY:
396 return BPF_TRAMP_FENTRY;
KP Singhae240822020-03-04 20:18:49 +0100397 case BPF_MODIFY_RETURN:
398 return BPF_TRAMP_MODIFY_RETURN;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800399 case BPF_TRACE_FEXIT:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800400 return BPF_TRAMP_FEXIT;
KP Singh9e4e01d2020-03-29 01:43:52 +0100401 case BPF_LSM_MAC:
402 if (!prog->aux->attach_func_proto->type)
403 /* The function returns void, we cannot modify its
404 * return value.
405 */
406 return BPF_TRAMP_FEXIT;
407 else
408 return BPF_TRAMP_MODIFY_RETURN;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800409 default:
410 return BPF_TRAMP_REPLACE;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800411 }
412}
413
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200414int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800415{
416 enum bpf_tramp_prog_type kind;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800417 int err = 0;
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800418 int cnt;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800419
KP Singh9e4e01d2020-03-29 01:43:52 +0100420 kind = bpf_attach_type_to_tramp(prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800421 mutex_lock(&tr->mutex);
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800422 if (tr->extension_prog) {
423 /* cannot attach fentry/fexit if extension prog is attached.
424 * cannot overwrite extension prog either.
425 */
426 err = -EBUSY;
427 goto out;
428 }
429 cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT];
430 if (kind == BPF_TRAMP_REPLACE) {
431 /* Cannot attach extension if fentry/fexit are in use. */
432 if (cnt) {
433 err = -EBUSY;
434 goto out;
435 }
436 tr->extension_prog = prog;
437 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
438 prog->bpf_func);
439 goto out;
440 }
441 if (cnt >= BPF_MAX_TRAMP_PROGS) {
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800442 err = -E2BIG;
443 goto out;
444 }
445 if (!hlist_unhashed(&prog->aux->tramp_hlist)) {
446 /* prog already linked */
447 err = -EBUSY;
448 goto out;
449 }
450 hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]);
451 tr->progs_cnt[kind]++;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200452 err = bpf_trampoline_update(tr);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800453 if (err) {
Jiri Olsaf3a95072021-04-14 21:51:41 +0200454 hlist_del_init(&prog->aux->tramp_hlist);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800455 tr->progs_cnt[kind]--;
456 }
457out:
458 mutex_unlock(&tr->mutex);
459 return err;
460}
461
462/* bpf_trampoline_unlink_prog() should never fail. */
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200463int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800464{
465 enum bpf_tramp_prog_type kind;
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800466 int err;
467
KP Singh9e4e01d2020-03-29 01:43:52 +0100468 kind = bpf_attach_type_to_tramp(prog);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800469 mutex_lock(&tr->mutex);
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800470 if (kind == BPF_TRAMP_REPLACE) {
471 WARN_ON_ONCE(!tr->extension_prog);
472 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
473 tr->extension_prog->bpf_func, NULL);
474 tr->extension_prog = NULL;
475 goto out;
476 }
Jiri Olsaf3a95072021-04-14 21:51:41 +0200477 hlist_del_init(&prog->aux->tramp_hlist);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800478 tr->progs_cnt[kind]--;
Toke Høiland-Jørgensen3aac1ea2020-09-29 14:45:50 +0200479 err = bpf_trampoline_update(tr);
Alexei Starovoitovbe8704f2020-01-20 16:53:46 -0800480out:
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800481 mutex_unlock(&tr->mutex);
482 return err;
483}
484
Toke Høiland-Jørgensenf7b12b62020-09-25 23:25:02 +0200485struct bpf_trampoline *bpf_trampoline_get(u64 key,
486 struct bpf_attach_target_info *tgt_info)
487{
488 struct bpf_trampoline *tr;
489
490 tr = bpf_trampoline_lookup(key);
491 if (!tr)
492 return NULL;
493
494 mutex_lock(&tr->mutex);
495 if (tr->func.addr)
496 goto out;
497
498 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel));
499 tr->func.addr = (void *)tgt_info->tgt_addr;
500out:
501 mutex_unlock(&tr->mutex);
502 return tr;
503}
504
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800505void bpf_trampoline_put(struct bpf_trampoline *tr)
506{
507 if (!tr)
508 return;
509 mutex_lock(&trampoline_mutex);
510 if (!refcount_dec_and_test(&tr->refcnt))
511 goto out;
512 WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
513 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY])))
514 goto out;
515 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
516 goto out;
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700517 /* This code will be executed even when the last bpf_tramp_image
518 * is alive. All progs are detached from the trampoline and the
519 * trampoline image is patched with jmp into epilogue to skip
520 * fexit progs. The fentry-only trampoline will be freed via
521 * multiple rcu callbacks.
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -0700522 */
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800523 hlist_del(&tr->hlist);
524 kfree(tr);
525out:
526 mutex_unlock(&trampoline_mutex);
527}
528
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800529#define NO_START_TIME 1
Song Liu856c02d2021-09-10 11:33:51 -0700530static __always_inline u64 notrace bpf_prog_start_time(void)
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800531{
532 u64 start = NO_START_TIME;
533
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800534 if (static_branch_unlikely(&bpf_stats_enabled_key)) {
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800535 start = sched_clock();
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800536 if (unlikely(!start))
537 start = NO_START_TIME;
538 }
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800539 return start;
540}
541
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -0800542static void notrace inc_misses_counter(struct bpf_prog *prog)
543{
544 struct bpf_prog_stats *stats;
545
546 stats = this_cpu_ptr(prog->stats);
547 u64_stats_update_begin(&stats->syncp);
Eric Dumazet61a0aba2021-10-26 14:41:33 -0700548 u64_stats_inc(&stats->misses);
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -0800549 u64_stats_update_end(&stats->syncp);
550}
551
Andrii Nakryikofb7dd8b2021-08-15 00:05:54 -0700552/* The logic is similar to bpf_prog_run(), but with an explicit
David Miller02ad0592020-02-24 15:01:45 +0100553 * rcu_read_lock() and migrate_disable() which are required
554 * for the trampoline. The macro is split into
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800555 * call __bpf_prog_enter
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800556 * call prog->bpf_func
557 * call __bpf_prog_exit
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800558 *
559 * __bpf_prog_enter returns:
560 * 0 - skip execution of the bpf prog
561 * 1 - execute bpf prog
Zhen Lei8fb33b62021-05-25 10:56:59 +0800562 * [2..MAX_U64] - execute bpf prog and record execution time.
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800563 * This is start time.
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800564 */
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800565u64 notrace __bpf_prog_enter(struct bpf_prog *prog)
Jules Irengedcce11d2020-03-11 01:09:01 +0000566 __acquires(RCU)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800567{
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800568 rcu_read_lock();
David Miller02ad0592020-02-24 15:01:45 +0100569 migrate_disable();
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -0800570 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
571 inc_misses_counter(prog);
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800572 return 0;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -0800573 }
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800574 return bpf_prog_start_time();
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800575}
576
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800577static void notrace update_prog_stats(struct bpf_prog *prog,
578 u64 start)
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800579{
580 struct bpf_prog_stats *stats;
581
582 if (static_branch_unlikely(&bpf_stats_enabled_key) &&
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800583 /* static_key could be enabled in __bpf_prog_enter*
584 * and disabled in __bpf_prog_exit*.
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800585 * And vice versa.
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800586 * Hence check that 'start' is valid.
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800587 */
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800588 start > NO_START_TIME) {
Eric Dumazetd9796172021-10-26 14:41:32 -0700589 unsigned long flags;
590
Alexei Starovoitov700d4792021-02-09 19:36:26 -0800591 stats = this_cpu_ptr(prog->stats);
Eric Dumazetd9796172021-10-26 14:41:32 -0700592 flags = u64_stats_update_begin_irqsave(&stats->syncp);
Eric Dumazet61a0aba2021-10-26 14:41:33 -0700593 u64_stats_inc(&stats->cnt);
594 u64_stats_add(&stats->nsecs, sched_clock() - start);
Eric Dumazetd9796172021-10-26 14:41:32 -0700595 u64_stats_update_end_irqrestore(&stats->syncp, flags);
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800596 }
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800597}
598
599void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
600 __releases(RCU)
601{
602 update_prog_stats(prog, start);
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800603 __this_cpu_dec(*(prog->active));
David Miller02ad0592020-02-24 15:01:45 +0100604 migrate_enable();
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800605 rcu_read_unlock();
606}
607
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800608u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog)
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -0700609{
610 rcu_read_lock_trace();
Alexei Starovoitov031d6e02021-02-09 19:36:27 -0800611 migrate_disable();
Alexei Starovoitovf56407f2020-08-31 13:16:51 -0700612 might_fault();
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -0800613 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
614 inc_misses_counter(prog);
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800615 return 0;
Alexei Starovoitov9ed9e9b2021-02-09 19:36:31 -0800616 }
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800617 return bpf_prog_start_time();
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -0700618}
619
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800620void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start)
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -0700621{
Alexei Starovoitovf2dd3b32021-02-09 19:36:28 -0800622 update_prog_stats(prog, start);
Alexei Starovoitovca06f552021-02-09 19:36:29 -0800623 __this_cpu_dec(*(prog->active));
Alexei Starovoitov031d6e02021-02-09 19:36:27 -0800624 migrate_enable();
Alexei Starovoitov1e6c62a2020-08-27 15:01:11 -0700625 rcu_read_unlock_trace();
626}
627
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700628void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr)
629{
630 percpu_ref_get(&tr->pcref);
631}
632
633void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr)
634{
635 percpu_ref_put(&tr->pcref);
636}
637
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800638int __weak
Alexei Starovoitove21aa342021-03-16 14:00:07 -0700639arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
Martin KaFai Lau85d33df2020-01-08 16:35:05 -0800640 const struct btf_func_model *m, u32 flags,
KP Singh88fd9e52020-03-04 20:18:47 +0100641 struct bpf_tramp_progs *tprogs,
Alexei Starovoitovfec56f52019-11-14 10:57:04 -0800642 void *orig_call)
643{
644 return -ENOTSUPP;
645}
646
647static int __init init_trampolines(void)
648{
649 int i;
650
651 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
652 INIT_HLIST_HEAD(&trampoline_table[i]);
653 return 0;
654}
655late_initcall(init_trampolines);