Lines Matching refs:tr
33 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex);
37 struct bpf_trampoline *tr = ops->private; in bpf_tramp_ftrace_ops_func() local
44 lockdep_assert_held_once(&tr->mutex); in bpf_tramp_ftrace_ops_func()
51 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && in bpf_tramp_ftrace_ops_func()
52 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) { in bpf_tramp_ftrace_ops_func()
53 if (WARN_ON_ONCE(tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY)) in bpf_tramp_ftrace_ops_func()
56 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; in bpf_tramp_ftrace_ops_func()
75 if (!mutex_trylock(&tr->mutex)) { in bpf_tramp_ftrace_ops_func()
85 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; in bpf_tramp_ftrace_ops_func()
87 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && in bpf_tramp_ftrace_ops_func()
88 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) in bpf_tramp_ftrace_ops_func()
89 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */); in bpf_tramp_ftrace_ops_func()
92 tr->flags &= ~BPF_TRAMP_F_SHARE_IPMODIFY; in bpf_tramp_ftrace_ops_func()
94 if (tr->flags & BPF_TRAMP_F_ORIG_STACK) in bpf_tramp_ftrace_ops_func()
95 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */); in bpf_tramp_ftrace_ops_func()
102 mutex_unlock(&tr->mutex); in bpf_tramp_ftrace_ops_func()
136 struct bpf_trampoline *tr; in bpf_trampoline_lookup() local
142 hlist_for_each_entry(tr, head, hlist) { in bpf_trampoline_lookup()
143 if (tr->key == key) { in bpf_trampoline_lookup()
144 refcount_inc(&tr->refcnt); in bpf_trampoline_lookup()
148 tr = kzalloc(sizeof(*tr), GFP_KERNEL); in bpf_trampoline_lookup()
149 if (!tr) in bpf_trampoline_lookup()
152 tr->fops = kzalloc(sizeof(struct ftrace_ops), GFP_KERNEL); in bpf_trampoline_lookup()
153 if (!tr->fops) { in bpf_trampoline_lookup()
154 kfree(tr); in bpf_trampoline_lookup()
155 tr = NULL; in bpf_trampoline_lookup()
158 tr->fops->private = tr; in bpf_trampoline_lookup()
159 tr->fops->ops_func = bpf_tramp_ftrace_ops_func; in bpf_trampoline_lookup()
162 tr->key = key; in bpf_trampoline_lookup()
163 INIT_HLIST_NODE(&tr->hlist); in bpf_trampoline_lookup()
164 hlist_add_head(&tr->hlist, head); in bpf_trampoline_lookup()
165 refcount_set(&tr->refcnt, 1); in bpf_trampoline_lookup()
166 mutex_init(&tr->mutex); in bpf_trampoline_lookup()
168 INIT_HLIST_HEAD(&tr->progs_hlist[i]); in bpf_trampoline_lookup()
171 return tr; in bpf_trampoline_lookup()
174 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr) in unregister_fentry() argument
176 void *ip = tr->func.addr; in unregister_fentry()
179 if (tr->func.ftrace_managed) in unregister_fentry()
180 ret = unregister_ftrace_direct(tr->fops, (long)old_addr, false); in unregister_fentry()
187 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr, in modify_fentry() argument
190 void *ip = tr->func.addr; in modify_fentry()
193 if (tr->func.ftrace_managed) { in modify_fentry()
195 ret = modify_ftrace_direct(tr->fops, (long)new_addr); in modify_fentry()
197 ret = modify_ftrace_direct_nolock(tr->fops, (long)new_addr); in modify_fentry()
205 static int register_fentry(struct bpf_trampoline *tr, void *new_addr) in register_fentry() argument
207 void *ip = tr->func.addr; in register_fentry()
213 if (!tr->fops) in register_fentry()
215 tr->func.ftrace_managed = true; in register_fentry()
218 if (tr->func.ftrace_managed) { in register_fentry()
219 ftrace_set_filter_ip(tr->fops, (unsigned long)ip, 0, 1); in register_fentry()
220 ret = register_ftrace_direct(tr->fops, (long)new_addr); in register_fentry()
229 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) in bpf_trampoline_get_progs() argument
242 tlinks[kind].nr_links = tr->progs_cnt[kind]; in bpf_trampoline_get_progs()
243 *total += tr->progs_cnt[kind]; in bpf_trampoline_get_progs()
246 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) { in bpf_trampoline_get_progs()
393 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex) in bpf_trampoline_update() argument
397 u32 orig_flags = tr->flags; in bpf_trampoline_update()
401 tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg); in bpf_trampoline_update()
406 err = unregister_fentry(tr, tr->cur_image->image); in bpf_trampoline_update()
407 bpf_tramp_image_put(tr->cur_image); in bpf_trampoline_update()
408 tr->cur_image = NULL; in bpf_trampoline_update()
413 tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX); in bpf_trampoline_update()
420 tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; in bpf_trampoline_update()
422 tr->flags |= BPF_TRAMP_F_RESTORE_REGS; in bpf_trampoline_update()
426 tr->flags |= BPF_TRAMP_F_IP_ARG; in bpf_trampoline_update()
430 if ((tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) && in bpf_trampoline_update()
431 (tr->flags & BPF_TRAMP_F_CALL_ORIG)) in bpf_trampoline_update()
432 tr->flags |= BPF_TRAMP_F_ORIG_STACK; in bpf_trampoline_update()
435 size = arch_bpf_trampoline_size(&tr->func.model, tr->flags, in bpf_trampoline_update()
436 tlinks, tr->func.addr); in bpf_trampoline_update()
447 im = bpf_tramp_image_alloc(tr->key, size); in bpf_trampoline_update()
454 &tr->func.model, tr->flags, tlinks, in bpf_trampoline_update()
455 tr->func.addr); in bpf_trampoline_update()
463 WARN_ON(tr->cur_image && total == 0); in bpf_trampoline_update()
464 if (tr->cur_image) in bpf_trampoline_update()
466 err = modify_fentry(tr, tr->cur_image->image, im->image, lock_direct_mutex); in bpf_trampoline_update()
469 err = register_fentry(tr, im->image); in bpf_trampoline_update()
478 tr->fops->func = NULL; in bpf_trampoline_update()
479 tr->fops->trampoline = 0; in bpf_trampoline_update()
489 if (tr->cur_image) in bpf_trampoline_update()
490 bpf_tramp_image_put(tr->cur_image); in bpf_trampoline_update()
491 tr->cur_image = im; in bpf_trampoline_update()
495 tr->flags = orig_flags; in bpf_trampoline_update()
526 static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr) in __bpf_trampoline_link_prog() argument
534 if (tr->extension_prog) in __bpf_trampoline_link_prog()
541 cnt += tr->progs_cnt[i]; in __bpf_trampoline_link_prog()
547 tr->extension_prog = link->link.prog; in __bpf_trampoline_link_prog()
548 return bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL, in __bpf_trampoline_link_prog()
556 hlist_for_each_entry(link_exiting, &tr->progs_hlist[kind], tramp_hlist) { in __bpf_trampoline_link_prog()
563 hlist_add_head(&link->tramp_hlist, &tr->progs_hlist[kind]); in __bpf_trampoline_link_prog()
564 tr->progs_cnt[kind]++; in __bpf_trampoline_link_prog()
565 err = bpf_trampoline_update(tr, true /* lock_direct_mutex */); in __bpf_trampoline_link_prog()
568 tr->progs_cnt[kind]--; in __bpf_trampoline_link_prog()
573 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr) in bpf_trampoline_link_prog() argument
577 mutex_lock(&tr->mutex); in bpf_trampoline_link_prog()
578 err = __bpf_trampoline_link_prog(link, tr); in bpf_trampoline_link_prog()
579 mutex_unlock(&tr->mutex); in bpf_trampoline_link_prog()
583 static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr) in __bpf_trampoline_unlink_prog() argument
590 WARN_ON_ONCE(!tr->extension_prog); in __bpf_trampoline_unlink_prog()
591 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, in __bpf_trampoline_unlink_prog()
592 tr->extension_prog->bpf_func, NULL); in __bpf_trampoline_unlink_prog()
593 tr->extension_prog = NULL; in __bpf_trampoline_unlink_prog()
597 tr->progs_cnt[kind]--; in __bpf_trampoline_unlink_prog()
598 return bpf_trampoline_update(tr, true /* lock_direct_mutex */); in __bpf_trampoline_unlink_prog()
602 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr) in bpf_trampoline_unlink_prog() argument
606 mutex_lock(&tr->mutex); in bpf_trampoline_unlink_prog()
607 err = __bpf_trampoline_unlink_prog(link, tr); in bpf_trampoline_unlink_prog()
608 mutex_unlock(&tr->mutex); in bpf_trampoline_unlink_prog()
674 static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr, in cgroup_shim_find() argument
681 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) { in cgroup_shim_find()
697 struct bpf_trampoline *tr; in bpf_trampoline_link_cgroup_shim() local
712 tr = bpf_trampoline_get(key, &tgt_info); in bpf_trampoline_link_cgroup_shim()
713 if (!tr) in bpf_trampoline_link_cgroup_shim()
716 mutex_lock(&tr->mutex); in bpf_trampoline_link_cgroup_shim()
718 shim_link = cgroup_shim_find(tr, bpf_func); in bpf_trampoline_link_cgroup_shim()
723 mutex_unlock(&tr->mutex); in bpf_trampoline_link_cgroup_shim()
724 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ in bpf_trampoline_link_cgroup_shim()
736 err = __bpf_trampoline_link_prog(&shim_link->link, tr); in bpf_trampoline_link_cgroup_shim()
740 shim_link->trampoline = tr; in bpf_trampoline_link_cgroup_shim()
743 mutex_unlock(&tr->mutex); in bpf_trampoline_link_cgroup_shim()
747 mutex_unlock(&tr->mutex); in bpf_trampoline_link_cgroup_shim()
753 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ in bpf_trampoline_link_cgroup_shim()
761 struct bpf_trampoline *tr; in bpf_trampoline_unlink_cgroup_shim() local
769 tr = bpf_trampoline_lookup(key); in bpf_trampoline_unlink_cgroup_shim()
770 if (WARN_ON_ONCE(!tr)) in bpf_trampoline_unlink_cgroup_shim()
773 mutex_lock(&tr->mutex); in bpf_trampoline_unlink_cgroup_shim()
774 shim_link = cgroup_shim_find(tr, bpf_func); in bpf_trampoline_unlink_cgroup_shim()
775 mutex_unlock(&tr->mutex); in bpf_trampoline_unlink_cgroup_shim()
780 bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */ in bpf_trampoline_unlink_cgroup_shim()
787 struct bpf_trampoline *tr; in bpf_trampoline_get() local
789 tr = bpf_trampoline_lookup(key); in bpf_trampoline_get()
790 if (!tr) in bpf_trampoline_get()
793 mutex_lock(&tr->mutex); in bpf_trampoline_get()
794 if (tr->func.addr) in bpf_trampoline_get()
797 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); in bpf_trampoline_get()
798 tr->func.addr = (void *)tgt_info->tgt_addr; in bpf_trampoline_get()
800 mutex_unlock(&tr->mutex); in bpf_trampoline_get()
801 return tr; in bpf_trampoline_get()
804 void bpf_trampoline_put(struct bpf_trampoline *tr) in bpf_trampoline_put() argument
808 if (!tr) in bpf_trampoline_put()
811 if (!refcount_dec_and_test(&tr->refcnt)) in bpf_trampoline_put()
813 WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); in bpf_trampoline_put()
816 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i]))) in bpf_trampoline_put()
825 hlist_del(&tr->hlist); in bpf_trampoline_put()
826 if (tr->fops) { in bpf_trampoline_put()
827 ftrace_free_filter(tr->fops); in bpf_trampoline_put()
828 kfree(tr->fops); in bpf_trampoline_put()
830 kfree(tr); in bpf_trampoline_put()
1008 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr) in __bpf_tramp_enter() argument
1010 percpu_ref_get(&tr->pcref); in __bpf_tramp_enter()
1013 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr) in __bpf_tramp_exit() argument
1015 percpu_ref_put(&tr->pcref); in __bpf_tramp_exit()