1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2019 Facebook */
3 #include <linux/hash.h>
5 #include <linux/filter.h>
6 #include <linux/ftrace.h>
7 #include <linux/rbtree_latch.h>
8 #include <linux/perf_event.h>
10 #include <linux/rcupdate_trace.h>
11 #include <linux/rcupdate_wait.h>
12 #include <linux/module.h>
13 #include <linux/static_call.h>
14 #include <linux/bpf_verifier.h>
15 #include <linux/bpf_lsm.h>
16 #include <linux/delay.h>
18 /* dummy _ops. The verifier will operate on target program's ops. */
19 const struct bpf_verifier_ops bpf_extension_verifier_ops = {
21 const struct bpf_prog_ops bpf_extension_prog_ops = {
24 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
25 #define TRAMPOLINE_HASH_BITS 10
26 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
28 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
30 /* serializes access to trampoline_table */
31 static DEFINE_MUTEX(trampoline_mutex);
33 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
34 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex);
36 static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, enum ftrace_ops_cmd cmd)
38 struct bpf_trampoline *tr = ops->private;
41 if (cmd == FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF) {
42 /* This is called inside register_ftrace_direct_multi(), so
43 * tr->mutex is already locked.
45 lockdep_assert_held_once(&tr->mutex);
47 /* Instead of updating the trampoline here, we propagate
48 * -EAGAIN to register_ftrace_direct_multi(). Then we can
49 * retry register_ftrace_direct_multi() after updating the
52 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) &&
53 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) {
54 if (WARN_ON_ONCE(tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY))
57 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY;
64 /* The normal locking order is
65 * tr->mutex => direct_mutex (ftrace.c) => ftrace_lock (ftrace.c)
67 * The following two commands are called from
69 * prepare_direct_functions_for_ipmodify
70 * cleanup_direct_functions_after_ipmodify
72 * In both cases, direct_mutex is already locked. Use
73 * mutex_trylock(&tr->mutex) to avoid deadlock in race condition
74 * (something else is making changes to this same trampoline).
76 if (!mutex_trylock(&tr->mutex)) {
77 /* sleep 1 ms to make sure whatever holding tr->mutex makes
85 case FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER:
86 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY;
88 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) &&
89 !(tr->flags & BPF_TRAMP_F_ORIG_STACK))
90 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */);
92 case FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER:
93 tr->flags &= ~BPF_TRAMP_F_SHARE_IPMODIFY;
95 if (tr->flags & BPF_TRAMP_F_ORIG_STACK)
96 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */);
103 mutex_unlock(&tr->mutex);
108 bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
110 enum bpf_attach_type eatype = prog->expected_attach_type;
111 enum bpf_prog_type ptype = prog->type;
113 return (ptype == BPF_PROG_TYPE_TRACING &&
114 (eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT ||
115 eatype == BPF_MODIFY_RETURN)) ||
116 (ptype == BPF_PROG_TYPE_LSM && eatype == BPF_LSM_MAC);
119 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
121 ksym->start = (unsigned long) data;
122 ksym->end = ksym->start + PAGE_SIZE;
124 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
125 PAGE_SIZE, false, ksym->name);
128 void bpf_image_ksym_del(struct bpf_ksym *ksym)
131 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
132 PAGE_SIZE, true, ksym->name);
135 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
137 struct bpf_trampoline *tr;
138 struct hlist_head *head;
141 mutex_lock(&trampoline_mutex);
142 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
143 hlist_for_each_entry(tr, head, hlist) {
144 if (tr->key == key) {
145 refcount_inc(&tr->refcnt);
149 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
152 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
153 tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL);
159 tr->fops->private = tr;
160 tr->fops->ops_func = bpf_tramp_ftrace_ops_func;
164 INIT_HLIST_NODE(&tr->hlist);
165 hlist_add_head(&tr->hlist, head);
166 refcount_set(&tr->refcnt, 1);
167 mutex_init(&tr->mutex);
168 for (i = 0; i < BPF_TRAMP_MAX; i++)
169 INIT_HLIST_HEAD(&tr->progs_hlist[i]);
171 mutex_unlock(&trampoline_mutex);
175 static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
181 mod = __module_text_address((unsigned long) tr->func.addr);
182 if (mod && !try_module_get(mod))
189 static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
195 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
197 void *ip = tr->func.addr;
200 if (tr->func.ftrace_managed)
201 ret = unregister_ftrace_direct_multi(tr->fops, (long)old_addr);
203 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
206 bpf_trampoline_module_put(tr);
210 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr,
211 bool lock_direct_mutex)
213 void *ip = tr->func.addr;
216 if (tr->func.ftrace_managed) {
217 if (lock_direct_mutex)
218 ret = modify_ftrace_direct_multi(tr->fops, (long)new_addr);
220 ret = modify_ftrace_direct_multi_nolock(tr->fops, (long)new_addr);
222 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
227 /* first time registering */
228 static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
230 void *ip = tr->func.addr;
234 faddr = ftrace_location((unsigned long)ip);
238 tr->func.ftrace_managed = true;
241 if (bpf_trampoline_module_get(tr))
244 if (tr->func.ftrace_managed) {
245 ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1);
246 ret = register_ftrace_direct_multi(tr->fops, (long)new_addr);
248 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
252 bpf_trampoline_module_put(tr);
256 static struct bpf_tramp_links *
257 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)
259 struct bpf_tramp_link *link;
260 struct bpf_tramp_links *tlinks;
261 struct bpf_tramp_link **links;
265 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
267 return ERR_PTR(-ENOMEM);
269 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
270 tlinks[kind].nr_links = tr->progs_cnt[kind];
271 *total += tr->progs_cnt[kind];
272 links = tlinks[kind].links;
274 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) {
275 *ip_arg |= link->link.prog->call_get_func_ip;
282 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
284 struct bpf_tramp_image *im;
286 im = container_of(work, struct bpf_tramp_image, work);
287 bpf_image_ksym_del(&im->ksym);
288 bpf_jit_free_exec(im->image);
289 bpf_jit_uncharge_modmem(PAGE_SIZE);
290 percpu_ref_exit(&im->pcref);
294 /* callback, fexit step 3 or fentry step 2 */
295 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu)
297 struct bpf_tramp_image *im;
299 im = container_of(rcu, struct bpf_tramp_image, rcu);
300 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred);
301 schedule_work(&im->work);
304 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */
305 static void __bpf_tramp_image_release(struct percpu_ref *pcref)
307 struct bpf_tramp_image *im;
309 im = container_of(pcref, struct bpf_tramp_image, pcref);
310 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
313 /* callback, fexit or fentry step 1 */
314 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
316 struct bpf_tramp_image *im;
318 im = container_of(rcu, struct bpf_tramp_image, rcu);
319 if (im->ip_after_call)
320 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
321 percpu_ref_kill(&im->pcref);
323 /* the case of fentry trampoline */
324 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
327 static void bpf_tramp_image_put(struct bpf_tramp_image *im)
329 /* The trampoline image that calls original function is using:
330 * rcu_read_lock_trace to protect sleepable bpf progs
331 * rcu_read_lock to protect normal bpf progs
332 * percpu_ref to protect trampoline itself
333 * rcu tasks to protect trampoline asm not covered by percpu_ref
334 * (which are few asm insns before __bpf_tramp_enter and
335 * after __bpf_tramp_exit)
337 * The trampoline is unreachable before bpf_tramp_image_put().
339 * First, patch the trampoline to avoid calling into fexit progs.
340 * The progs will be freed even if the original function is still
341 * executing or sleeping.
342 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
343 * first few asm instructions to execute and call into
344 * __bpf_tramp_enter->percpu_ref_get.
345 * Then use percpu_ref_kill to wait for the trampoline and the original
346 * function to finish.
347 * Then use call_rcu_tasks() to make sure few asm insns in
348 * the trampoline epilogue are done as well.
350 * In !PREEMPT case the task that got interrupted in the first asm
351 * insns won't go through an RCU quiescent state which the
352 * percpu_ref_kill will be waiting for. Hence the first
353 * call_rcu_tasks() is not necessary.
355 if (im->ip_after_call) {
356 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP,
357 NULL, im->ip_epilogue);
359 if (IS_ENABLED(CONFIG_PREEMPTION))
360 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
362 percpu_ref_kill(&im->pcref);
366 /* The trampoline without fexit and fmod_ret progs doesn't call original
367 * function and doesn't use percpu_ref.
368 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
369 * Then use call_rcu_tasks() to wait for the rest of trampoline asm
372 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
375 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
377 struct bpf_tramp_image *im;
378 struct bpf_ksym *ksym;
382 im = kzalloc(sizeof(*im), GFP_KERNEL);
386 err = bpf_jit_charge_modmem(PAGE_SIZE);
391 im->image = image = bpf_jit_alloc_exec(PAGE_SIZE);
394 set_vm_flush_reset_perms(image);
396 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
401 INIT_LIST_HEAD_RCU(&ksym->lnode);
402 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key);
403 bpf_image_ksym_add(image, ksym);
407 bpf_jit_free_exec(im->image);
409 bpf_jit_uncharge_modmem(PAGE_SIZE);
416 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex)
418 struct bpf_tramp_image *im;
419 struct bpf_tramp_links *tlinks;
420 u32 orig_flags = tr->flags;
424 tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg);
426 return PTR_ERR(tlinks);
429 err = unregister_fentry(tr, tr->cur_image->image);
430 bpf_tramp_image_put(tr->cur_image);
431 tr->cur_image = NULL;
435 im = bpf_tramp_image_alloc(tr->key);
441 /* clear all bits except SHARE_IPMODIFY */
442 tr->flags &= BPF_TRAMP_F_SHARE_IPMODIFY;
444 if (tlinks[BPF_TRAMP_FEXIT].nr_links ||
445 tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links) {
446 /* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME
447 * should not be set together.
449 tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
451 tr->flags |= BPF_TRAMP_F_RESTORE_REGS;
455 tr->flags |= BPF_TRAMP_F_IP_ARG;
457 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
459 if ((tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) &&
460 (tr->flags & BPF_TRAMP_F_CALL_ORIG))
461 tr->flags |= BPF_TRAMP_F_ORIG_STACK;
464 err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
465 &tr->func.model, tr->flags, tlinks,
470 set_memory_ro((long)im->image, 1);
471 set_memory_x((long)im->image, 1);
473 WARN_ON(tr->cur_image && total == 0);
475 /* progs already running at this address */
476 err = modify_fentry(tr, tr->cur_image->image, im->image, lock_direct_mutex);
478 /* first time registering */
479 err = register_fentry(tr, im->image);
481 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
482 if (err == -EAGAIN) {
483 /* -EAGAIN from bpf_tramp_ftrace_ops_func. Now
484 * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the
485 * trampoline again, and retry register.
487 /* reset fops->func and fops->trampoline for re-register */
488 tr->fops->func = NULL;
489 tr->fops->trampoline = 0;
491 /* reset im->image memory attr for arch_prepare_bpf_trampoline */
492 set_memory_nx((long)im->image, 1);
493 set_memory_rw((long)im->image, 1);
501 bpf_tramp_image_put(tr->cur_image);
504 /* If any error happens, restore previous flags */
506 tr->flags = orig_flags;
511 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
513 switch (prog->expected_attach_type) {
514 case BPF_TRACE_FENTRY:
515 return BPF_TRAMP_FENTRY;
516 case BPF_MODIFY_RETURN:
517 return BPF_TRAMP_MODIFY_RETURN;
518 case BPF_TRACE_FEXIT:
519 return BPF_TRAMP_FEXIT;
521 if (!prog->aux->attach_func_proto->type)
522 /* The function returns void, we cannot modify its
525 return BPF_TRAMP_FEXIT;
527 return BPF_TRAMP_MODIFY_RETURN;
529 return BPF_TRAMP_REPLACE;
533 static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
535 enum bpf_tramp_prog_type kind;
536 struct bpf_tramp_link *link_exiting;
540 kind = bpf_attach_type_to_tramp(link->link.prog);
541 if (tr->extension_prog)
542 /* cannot attach fentry/fexit if extension prog is attached.
543 * cannot overwrite extension prog either.
547 for (i = 0; i < BPF_TRAMP_MAX; i++)
548 cnt += tr->progs_cnt[i];
550 if (kind == BPF_TRAMP_REPLACE) {
551 /* Cannot attach extension if fentry/fexit are in use. */
554 tr->extension_prog = link->link.prog;
555 return bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
556 link->link.prog->bpf_func);
558 if (cnt >= BPF_MAX_TRAMP_LINKS)
560 if (!hlist_unhashed(&link->tramp_hlist))
561 /* prog already linked */
563 hlist_for_each_entry(link_exiting, &tr->progs_hlist[kind], tramp_hlist) {
564 if (link_exiting->link.prog != link->link.prog)
566 /* prog already linked */
570 hlist_add_head(&link->tramp_hlist, &tr->progs_hlist[kind]);
571 tr->progs_cnt[kind]++;
572 err = bpf_trampoline_update(tr, true /* lock_direct_mutex */);
574 hlist_del_init(&link->tramp_hlist);
575 tr->progs_cnt[kind]--;
580 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
584 mutex_lock(&tr->mutex);
585 err = __bpf_trampoline_link_prog(link, tr);
586 mutex_unlock(&tr->mutex);
590 static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
592 enum bpf_tramp_prog_type kind;
595 kind = bpf_attach_type_to_tramp(link->link.prog);
596 if (kind == BPF_TRAMP_REPLACE) {
597 WARN_ON_ONCE(!tr->extension_prog);
598 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
599 tr->extension_prog->bpf_func, NULL);
600 tr->extension_prog = NULL;
603 hlist_del_init(&link->tramp_hlist);
604 tr->progs_cnt[kind]--;
605 return bpf_trampoline_update(tr, true /* lock_direct_mutex */);
608 /* bpf_trampoline_unlink_prog() should never fail. */
609 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
613 mutex_lock(&tr->mutex);
614 err = __bpf_trampoline_unlink_prog(link, tr);
615 mutex_unlock(&tr->mutex);
619 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM)
620 static void bpf_shim_tramp_link_release(struct bpf_link *link)
622 struct bpf_shim_tramp_link *shim_link =
623 container_of(link, struct bpf_shim_tramp_link, link.link);
625 /* paired with 'shim_link->trampoline = tr' in bpf_trampoline_link_cgroup_shim */
626 if (!shim_link->trampoline)
629 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&shim_link->link, shim_link->trampoline));
630 bpf_trampoline_put(shim_link->trampoline);
633 static void bpf_shim_tramp_link_dealloc(struct bpf_link *link)
635 struct bpf_shim_tramp_link *shim_link =
636 container_of(link, struct bpf_shim_tramp_link, link.link);
641 static const struct bpf_link_ops bpf_shim_tramp_link_lops = {
642 .release = bpf_shim_tramp_link_release,
643 .dealloc = bpf_shim_tramp_link_dealloc,
646 static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog,
650 struct bpf_shim_tramp_link *shim_link = NULL;
653 shim_link = kzalloc(sizeof(*shim_link), GFP_USER);
657 p = bpf_prog_alloc(1, 0);
664 p->bpf_func = bpf_func;
666 p->aux->cgroup_atype = cgroup_atype;
667 p->aux->attach_func_proto = prog->aux->attach_func_proto;
668 p->aux->attach_btf_id = prog->aux->attach_btf_id;
669 p->aux->attach_btf = prog->aux->attach_btf;
670 btf_get(p->aux->attach_btf);
671 p->type = BPF_PROG_TYPE_LSM;
672 p->expected_attach_type = BPF_LSM_MAC;
674 bpf_link_init(&shim_link->link.link, BPF_LINK_TYPE_UNSPEC,
675 &bpf_shim_tramp_link_lops, p);
676 bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype);
681 static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr,
684 struct bpf_tramp_link *link;
687 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
688 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) {
689 struct bpf_prog *p = link->link.prog;
691 if (p->bpf_func == bpf_func)
692 return container_of(link, struct bpf_shim_tramp_link, link);
699 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog,
702 struct bpf_shim_tramp_link *shim_link = NULL;
703 struct bpf_attach_target_info tgt_info = {};
704 struct bpf_trampoline *tr;
709 err = bpf_check_attach_target(NULL, prog, NULL,
710 prog->aux->attach_btf_id,
715 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf,
716 prog->aux->attach_btf_id);
718 bpf_lsm_find_cgroup_shim(prog, &bpf_func);
719 tr = bpf_trampoline_get(key, &tgt_info);
723 mutex_lock(&tr->mutex);
725 shim_link = cgroup_shim_find(tr, bpf_func);
727 /* Reusing existing shim attached by the other program. */
728 bpf_link_inc(&shim_link->link.link);
730 mutex_unlock(&tr->mutex);
731 bpf_trampoline_put(tr); /* bpf_trampoline_get above */
735 /* Allocate and install new shim. */
737 shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype);
743 err = __bpf_trampoline_link_prog(&shim_link->link, tr);
747 shim_link->trampoline = tr;
748 /* note, we're still holding tr refcnt from above */
750 mutex_unlock(&tr->mutex);
754 mutex_unlock(&tr->mutex);
757 bpf_link_put(&shim_link->link.link);
759 /* have to release tr while _not_ holding its mutex */
760 bpf_trampoline_put(tr); /* bpf_trampoline_get above */
765 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog)
767 struct bpf_shim_tramp_link *shim_link = NULL;
768 struct bpf_trampoline *tr;
772 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf,
773 prog->aux->attach_btf_id);
775 bpf_lsm_find_cgroup_shim(prog, &bpf_func);
776 tr = bpf_trampoline_lookup(key);
777 if (WARN_ON_ONCE(!tr))
780 mutex_lock(&tr->mutex);
781 shim_link = cgroup_shim_find(tr, bpf_func);
782 mutex_unlock(&tr->mutex);
785 bpf_link_put(&shim_link->link.link);
787 bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */
791 struct bpf_trampoline *bpf_trampoline_get(u64 key,
792 struct bpf_attach_target_info *tgt_info)
794 struct bpf_trampoline *tr;
796 tr = bpf_trampoline_lookup(key);
800 mutex_lock(&tr->mutex);
804 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel));
805 tr->func.addr = (void *)tgt_info->tgt_addr;
807 mutex_unlock(&tr->mutex);
811 void bpf_trampoline_put(struct bpf_trampoline *tr)
817 mutex_lock(&trampoline_mutex);
818 if (!refcount_dec_and_test(&tr->refcnt))
820 WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
822 for (i = 0; i < BPF_TRAMP_MAX; i++)
823 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
826 /* This code will be executed even when the last bpf_tramp_image
827 * is alive. All progs are detached from the trampoline and the
828 * trampoline image is patched with jmp into epilogue to skip
829 * fexit progs. The fentry-only trampoline will be freed via
830 * multiple rcu callbacks.
832 hlist_del(&tr->hlist);
834 ftrace_free_filter(tr->fops);
839 mutex_unlock(&trampoline_mutex);
842 #define NO_START_TIME 1
843 static __always_inline u64 notrace bpf_prog_start_time(void)
845 u64 start = NO_START_TIME;
847 if (static_branch_unlikely(&bpf_stats_enabled_key)) {
848 start = sched_clock();
849 if (unlikely(!start))
850 start = NO_START_TIME;
855 /* The logic is similar to bpf_prog_run(), but with an explicit
856 * rcu_read_lock() and migrate_disable() which are required
857 * for the trampoline. The macro is split into
858 * call __bpf_prog_enter
859 * call prog->bpf_func
860 * call __bpf_prog_exit
862 * __bpf_prog_enter returns:
863 * 0 - skip execution of the bpf prog
864 * 1 - execute bpf prog
865 * [2..MAX_U64] - execute bpf prog and record execution time.
866 * This is start time.
868 u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
874 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
876 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
877 bpf_prog_inc_misses_counter(prog);
880 return bpf_prog_start_time();
883 static void notrace update_prog_stats(struct bpf_prog *prog,
886 struct bpf_prog_stats *stats;
888 if (static_branch_unlikely(&bpf_stats_enabled_key) &&
889 /* static_key could be enabled in __bpf_prog_enter*
890 * and disabled in __bpf_prog_exit*.
892 * Hence check that 'start' is valid.
894 start > NO_START_TIME) {
897 stats = this_cpu_ptr(prog->stats);
898 flags = u64_stats_update_begin_irqsave(&stats->syncp);
899 u64_stats_inc(&stats->cnt);
900 u64_stats_add(&stats->nsecs, sched_clock() - start);
901 u64_stats_update_end_irqrestore(&stats->syncp, flags);
905 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx)
908 bpf_reset_run_ctx(run_ctx->saved_run_ctx);
910 update_prog_stats(prog, start);
911 this_cpu_dec(*(prog->active));
916 u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog,
917 struct bpf_tramp_run_ctx *run_ctx)
920 /* Runtime stats are exported via actual BPF_LSM_CGROUP
921 * programs, not the shims.
926 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
928 return NO_START_TIME;
931 void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start,
932 struct bpf_tramp_run_ctx *run_ctx)
935 bpf_reset_run_ctx(run_ctx->saved_run_ctx);
941 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
943 rcu_read_lock_trace();
947 if (unlikely(this_cpu_inc_return(*(prog->active)) != 1)) {
948 bpf_prog_inc_misses_counter(prog);
952 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
954 return bpf_prog_start_time();
957 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
958 struct bpf_tramp_run_ctx *run_ctx)
960 bpf_reset_run_ctx(run_ctx->saved_run_ctx);
962 update_prog_stats(prog, start);
963 this_cpu_dec(*(prog->active));
965 rcu_read_unlock_trace();
968 u64 notrace __bpf_prog_enter_struct_ops(struct bpf_prog *prog,
969 struct bpf_tramp_run_ctx *run_ctx)
975 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
977 return bpf_prog_start_time();
980 void notrace __bpf_prog_exit_struct_ops(struct bpf_prog *prog, u64 start,
981 struct bpf_tramp_run_ctx *run_ctx)
984 bpf_reset_run_ctx(run_ctx->saved_run_ctx);
986 update_prog_stats(prog, start);
991 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr)
993 percpu_ref_get(&tr->pcref);
996 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr)
998 percpu_ref_put(&tr->pcref);
1002 arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
1003 const struct btf_func_model *m, u32 flags,
1004 struct bpf_tramp_links *tlinks,
1010 static int __init init_trampolines(void)
1014 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
1015 INIT_HLIST_HEAD(&trampoline_table[i]);
1018 late_initcall(init_trampolines);