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>
15 /* dummy _ops. The verifier will operate on target program's ops. */
16 const struct bpf_verifier_ops bpf_extension_verifier_ops = {
18 const struct bpf_prog_ops bpf_extension_prog_ops = {
21 /* 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)
25 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
27 /* serializes access to trampoline_table */
28 static DEFINE_MUTEX(trampoline_mutex);
30 bool bpf_prog_has_trampoline(const struct bpf_prog *prog)
32 enum bpf_attach_type eatype = prog->expected_attach_type;
33 enum bpf_prog_type ptype = prog->type;
35 return (ptype == BPF_PROG_TYPE_TRACING &&
36 (eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT ||
37 eatype == BPF_MODIFY_RETURN)) ||
38 (ptype == BPF_PROG_TYPE_LSM && eatype == BPF_LSM_MAC);
41 void *bpf_jit_alloc_exec_page(void)
45 image = bpf_jit_alloc_exec(PAGE_SIZE);
49 set_vm_flush_reset_perms(image);
50 /* Keep image as writeable. The alternative is to keep flipping ro/rw
51 * every time new program is attached or detached.
53 set_memory_x((long)image, 1);
57 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
59 ksym->start = (unsigned long) data;
60 ksym->end = ksym->start + PAGE_SIZE;
62 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
63 PAGE_SIZE, false, ksym->name);
66 void bpf_image_ksym_del(struct bpf_ksym *ksym)
69 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
70 PAGE_SIZE, true, ksym->name);
73 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
75 struct bpf_trampoline *tr;
76 struct hlist_head *head;
79 mutex_lock(&trampoline_mutex);
80 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
81 hlist_for_each_entry(tr, head, hlist) {
83 refcount_inc(&tr->refcnt);
87 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
92 INIT_HLIST_NODE(&tr->hlist);
93 hlist_add_head(&tr->hlist, head);
94 refcount_set(&tr->refcnt, 1);
95 mutex_init(&tr->mutex);
96 for (i = 0; i < BPF_TRAMP_MAX; i++)
97 INIT_HLIST_HEAD(&tr->progs_hlist[i]);
99 mutex_unlock(&trampoline_mutex);
103 static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
109 mod = __module_text_address((unsigned long) tr->func.addr);
110 if (mod && !try_module_get(mod))
117 static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
123 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr)
125 void *ip = tr->func.addr;
128 if (tr->func.ftrace_managed)
129 ret = unregister_ftrace_direct((long)ip, (long)old_addr);
131 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL);
134 bpf_trampoline_module_put(tr);
138 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr)
140 void *ip = tr->func.addr;
143 if (tr->func.ftrace_managed)
144 ret = modify_ftrace_direct((long)ip, (long)old_addr, (long)new_addr);
146 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr);
150 /* first time registering */
151 static int register_fentry(struct bpf_trampoline *tr, void *new_addr)
153 void *ip = tr->func.addr;
157 faddr = ftrace_location((unsigned long)ip);
159 tr->func.ftrace_managed = true;
161 if (bpf_trampoline_module_get(tr))
164 if (tr->func.ftrace_managed)
165 ret = register_ftrace_direct((long)ip, (long)new_addr);
167 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr);
170 bpf_trampoline_module_put(tr);
174 static struct bpf_tramp_links *
175 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg)
177 struct bpf_tramp_link *link;
178 struct bpf_tramp_links *tlinks;
179 struct bpf_tramp_link **links;
183 tlinks = kcalloc(BPF_TRAMP_MAX, sizeof(*tlinks), GFP_KERNEL);
185 return ERR_PTR(-ENOMEM);
187 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
188 tlinks[kind].nr_links = tr->progs_cnt[kind];
189 *total += tr->progs_cnt[kind];
190 links = tlinks[kind].links;
192 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) {
193 *ip_arg |= link->link.prog->call_get_func_ip;
200 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
202 struct bpf_tramp_image *im;
204 im = container_of(work, struct bpf_tramp_image, work);
205 bpf_image_ksym_del(&im->ksym);
206 bpf_jit_free_exec(im->image);
207 bpf_jit_uncharge_modmem(PAGE_SIZE);
208 percpu_ref_exit(&im->pcref);
212 /* callback, fexit step 3 or fentry step 2 */
213 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu)
215 struct bpf_tramp_image *im;
217 im = container_of(rcu, struct bpf_tramp_image, rcu);
218 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred);
219 schedule_work(&im->work);
222 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */
223 static void __bpf_tramp_image_release(struct percpu_ref *pcref)
225 struct bpf_tramp_image *im;
227 im = container_of(pcref, struct bpf_tramp_image, pcref);
228 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
231 /* callback, fexit or fentry step 1 */
232 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
234 struct bpf_tramp_image *im;
236 im = container_of(rcu, struct bpf_tramp_image, rcu);
237 if (im->ip_after_call)
238 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
239 percpu_ref_kill(&im->pcref);
241 /* the case of fentry trampoline */
242 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
245 static void bpf_tramp_image_put(struct bpf_tramp_image *im)
247 /* The trampoline image that calls original function is using:
248 * rcu_read_lock_trace to protect sleepable bpf progs
249 * rcu_read_lock to protect normal bpf progs
250 * percpu_ref to protect trampoline itself
251 * rcu tasks to protect trampoline asm not covered by percpu_ref
252 * (which are few asm insns before __bpf_tramp_enter and
253 * after __bpf_tramp_exit)
255 * The trampoline is unreachable before bpf_tramp_image_put().
257 * First, patch the trampoline to avoid calling into fexit progs.
258 * The progs will be freed even if the original function is still
259 * executing or sleeping.
260 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
261 * first few asm instructions to execute and call into
262 * __bpf_tramp_enter->percpu_ref_get.
263 * Then use percpu_ref_kill to wait for the trampoline and the original
264 * function to finish.
265 * Then use call_rcu_tasks() to make sure few asm insns in
266 * the trampoline epilogue are done as well.
268 * In !PREEMPT case the task that got interrupted in the first asm
269 * insns won't go through an RCU quiescent state which the
270 * percpu_ref_kill will be waiting for. Hence the first
271 * call_rcu_tasks() is not necessary.
273 if (im->ip_after_call) {
274 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP,
275 NULL, im->ip_epilogue);
277 if (IS_ENABLED(CONFIG_PREEMPTION))
278 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
280 percpu_ref_kill(&im->pcref);
284 /* The trampoline without fexit and fmod_ret progs doesn't call original
285 * function and doesn't use percpu_ref.
286 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
287 * Then use call_rcu_tasks() to wait for the rest of trampoline asm
290 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
293 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
295 struct bpf_tramp_image *im;
296 struct bpf_ksym *ksym;
300 im = kzalloc(sizeof(*im), GFP_KERNEL);
304 err = bpf_jit_charge_modmem(PAGE_SIZE);
309 im->image = image = bpf_jit_alloc_exec_page();
313 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
318 INIT_LIST_HEAD_RCU(&ksym->lnode);
319 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx);
320 bpf_image_ksym_add(image, ksym);
324 bpf_jit_free_exec(im->image);
326 bpf_jit_uncharge_modmem(PAGE_SIZE);
333 static int bpf_trampoline_update(struct bpf_trampoline *tr)
335 struct bpf_tramp_image *im;
336 struct bpf_tramp_links *tlinks;
337 u32 flags = BPF_TRAMP_F_RESTORE_REGS;
341 tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg);
343 return PTR_ERR(tlinks);
346 err = unregister_fentry(tr, tr->cur_image->image);
347 bpf_tramp_image_put(tr->cur_image);
348 tr->cur_image = NULL;
353 im = bpf_tramp_image_alloc(tr->key, tr->selector);
359 if (tlinks[BPF_TRAMP_FEXIT].nr_links ||
360 tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links)
361 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
364 flags |= BPF_TRAMP_F_IP_ARG;
366 err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
367 &tr->func.model, flags, tlinks,
372 WARN_ON(tr->cur_image && tr->selector == 0);
373 WARN_ON(!tr->cur_image && tr->selector);
375 /* progs already running at this address */
376 err = modify_fentry(tr, tr->cur_image->image, im->image);
378 /* first time registering */
379 err = register_fentry(tr, im->image);
383 bpf_tramp_image_put(tr->cur_image);
391 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
393 switch (prog->expected_attach_type) {
394 case BPF_TRACE_FENTRY:
395 return BPF_TRAMP_FENTRY;
396 case BPF_MODIFY_RETURN:
397 return BPF_TRAMP_MODIFY_RETURN;
398 case BPF_TRACE_FEXIT:
399 return BPF_TRAMP_FEXIT;
401 if (!prog->aux->attach_func_proto->type)
402 /* The function returns void, we cannot modify its
405 return BPF_TRAMP_FEXIT;
407 return BPF_TRAMP_MODIFY_RETURN;
409 return BPF_TRAMP_REPLACE;
413 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
415 enum bpf_tramp_prog_type kind;
416 struct bpf_tramp_link *link_exiting;
420 kind = bpf_attach_type_to_tramp(link->link.prog);
421 mutex_lock(&tr->mutex);
422 if (tr->extension_prog) {
423 /* cannot attach fentry/fexit if extension prog is attached.
424 * cannot overwrite extension prog either.
430 for (i = 0; i < BPF_TRAMP_MAX; i++)
431 cnt += tr->progs_cnt[i];
433 if (kind == BPF_TRAMP_REPLACE) {
434 /* Cannot attach extension if fentry/fexit are in use. */
439 tr->extension_prog = link->link.prog;
440 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
441 link->link.prog->bpf_func);
444 if (cnt >= BPF_MAX_TRAMP_LINKS) {
448 if (!hlist_unhashed(&link->tramp_hlist)) {
449 /* prog already linked */
453 hlist_for_each_entry(link_exiting, &tr->progs_hlist[kind], tramp_hlist) {
454 if (link_exiting->link.prog != link->link.prog)
456 /* prog already linked */
461 hlist_add_head(&link->tramp_hlist, &tr->progs_hlist[kind]);
462 tr->progs_cnt[kind]++;
463 err = bpf_trampoline_update(tr);
465 hlist_del_init(&link->tramp_hlist);
466 tr->progs_cnt[kind]--;
469 mutex_unlock(&tr->mutex);
473 /* bpf_trampoline_unlink_prog() should never fail. */
474 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr)
476 enum bpf_tramp_prog_type kind;
479 kind = bpf_attach_type_to_tramp(link->link.prog);
480 mutex_lock(&tr->mutex);
481 if (kind == BPF_TRAMP_REPLACE) {
482 WARN_ON_ONCE(!tr->extension_prog);
483 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
484 tr->extension_prog->bpf_func, NULL);
485 tr->extension_prog = NULL;
488 hlist_del_init(&link->tramp_hlist);
489 tr->progs_cnt[kind]--;
490 err = bpf_trampoline_update(tr);
492 mutex_unlock(&tr->mutex);
496 struct bpf_trampoline *bpf_trampoline_get(u64 key,
497 struct bpf_attach_target_info *tgt_info)
499 struct bpf_trampoline *tr;
501 tr = bpf_trampoline_lookup(key);
505 mutex_lock(&tr->mutex);
509 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel));
510 tr->func.addr = (void *)tgt_info->tgt_addr;
512 mutex_unlock(&tr->mutex);
516 void bpf_trampoline_put(struct bpf_trampoline *tr)
522 mutex_lock(&trampoline_mutex);
523 if (!refcount_dec_and_test(&tr->refcnt))
525 WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
527 for (i = 0; i < BPF_TRAMP_MAX; i++)
528 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i])))
531 /* This code will be executed even when the last bpf_tramp_image
532 * is alive. All progs are detached from the trampoline and the
533 * trampoline image is patched with jmp into epilogue to skip
534 * fexit progs. The fentry-only trampoline will be freed via
535 * multiple rcu callbacks.
537 hlist_del(&tr->hlist);
540 mutex_unlock(&trampoline_mutex);
543 #define NO_START_TIME 1
544 static __always_inline u64 notrace bpf_prog_start_time(void)
546 u64 start = NO_START_TIME;
548 if (static_branch_unlikely(&bpf_stats_enabled_key)) {
549 start = sched_clock();
550 if (unlikely(!start))
551 start = NO_START_TIME;
556 static void notrace inc_misses_counter(struct bpf_prog *prog)
558 struct bpf_prog_stats *stats;
561 stats = this_cpu_ptr(prog->stats);
562 flags = u64_stats_update_begin_irqsave(&stats->syncp);
563 u64_stats_inc(&stats->misses);
564 u64_stats_update_end_irqrestore(&stats->syncp, flags);
567 /* The logic is similar to bpf_prog_run(), but with an explicit
568 * rcu_read_lock() and migrate_disable() which are required
569 * for the trampoline. The macro is split into
570 * call __bpf_prog_enter
571 * call prog->bpf_func
572 * call __bpf_prog_exit
574 * __bpf_prog_enter returns:
575 * 0 - skip execution of the bpf prog
576 * 1 - execute bpf prog
577 * [2..MAX_U64] - execute bpf prog and record execution time.
578 * This is start time.
580 u64 notrace __bpf_prog_enter(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
586 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
588 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
589 inc_misses_counter(prog);
592 return bpf_prog_start_time();
595 static void notrace update_prog_stats(struct bpf_prog *prog,
598 struct bpf_prog_stats *stats;
600 if (static_branch_unlikely(&bpf_stats_enabled_key) &&
601 /* static_key could be enabled in __bpf_prog_enter*
602 * and disabled in __bpf_prog_exit*.
604 * Hence check that 'start' is valid.
606 start > NO_START_TIME) {
609 stats = this_cpu_ptr(prog->stats);
610 flags = u64_stats_update_begin_irqsave(&stats->syncp);
611 u64_stats_inc(&stats->cnt);
612 u64_stats_add(&stats->nsecs, sched_clock() - start);
613 u64_stats_update_end_irqrestore(&stats->syncp, flags);
617 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, struct bpf_tramp_run_ctx *run_ctx)
620 bpf_reset_run_ctx(run_ctx->saved_run_ctx);
622 update_prog_stats(prog, start);
623 __this_cpu_dec(*(prog->active));
628 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx)
630 rcu_read_lock_trace();
634 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
635 inc_misses_counter(prog);
639 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx);
641 return bpf_prog_start_time();
644 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start,
645 struct bpf_tramp_run_ctx *run_ctx)
647 bpf_reset_run_ctx(run_ctx->saved_run_ctx);
649 update_prog_stats(prog, start);
650 __this_cpu_dec(*(prog->active));
652 rcu_read_unlock_trace();
655 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr)
657 percpu_ref_get(&tr->pcref);
660 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr)
662 percpu_ref_put(&tr->pcref);
666 arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
667 const struct btf_func_model *m, u32 flags,
668 struct bpf_tramp_links *tlinks,
674 static int __init init_trampolines(void)
678 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
679 INIT_HLIST_HEAD(&trampoline_table[i]);
682 late_initcall(init_trampolines);