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>
14 /* dummy _ops. The verifier will operate on target program's ops. */
15 const struct bpf_verifier_ops bpf_extension_verifier_ops = {
17 const struct bpf_prog_ops bpf_extension_prog_ops = {
20 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
21 #define TRAMPOLINE_HASH_BITS 10
22 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
24 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
26 /* serializes access to trampoline_table */
27 static DEFINE_MUTEX(trampoline_mutex);
29 void *bpf_jit_alloc_exec_page(void)
33 image = bpf_jit_alloc_exec(PAGE_SIZE);
37 set_vm_flush_reset_perms(image);
38 /* Keep image as writeable. The alternative is to keep flipping ro/rw
39 * everytime new program is attached or detached.
41 set_memory_x((long)image, 1);
45 void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
47 ksym->start = (unsigned long) data;
48 ksym->end = ksym->start + PAGE_SIZE;
50 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
51 PAGE_SIZE, false, ksym->name);
54 void bpf_image_ksym_del(struct bpf_ksym *ksym)
57 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
58 PAGE_SIZE, true, ksym->name);
61 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
63 struct bpf_trampoline *tr;
64 struct hlist_head *head;
67 mutex_lock(&trampoline_mutex);
68 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
69 hlist_for_each_entry(tr, head, hlist) {
71 refcount_inc(&tr->refcnt);
75 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
80 INIT_HLIST_NODE(&tr->hlist);
81 hlist_add_head(&tr->hlist, head);
82 refcount_set(&tr->refcnt, 1);
83 mutex_init(&tr->mutex);
84 for (i = 0; i < BPF_TRAMP_MAX; i++)
85 INIT_HLIST_HEAD(&tr->progs_hlist[i]);
87 mutex_unlock(&trampoline_mutex);
91 static int bpf_trampoline_module_get(struct bpf_trampoline *tr)
97 mod = __module_text_address((unsigned long) tr->func.addr);
98 if (mod && !try_module_get(mod))
105 static void bpf_trampoline_module_put(struct bpf_trampoline *tr)
111 static int is_ftrace_location(void *ip)
115 addr = ftrace_location((long)ip);
118 if (WARN_ON_ONCE(addr != (long)ip))
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;
156 ret = is_ftrace_location(ip);
159 tr->func.ftrace_managed = ret;
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_progs *
175 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total)
177 const struct bpf_prog_aux *aux;
178 struct bpf_tramp_progs *tprogs;
179 struct bpf_prog **progs;
183 tprogs = kcalloc(BPF_TRAMP_MAX, sizeof(*tprogs), GFP_KERNEL);
185 return ERR_PTR(-ENOMEM);
187 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) {
188 tprogs[kind].nr_progs = tr->progs_cnt[kind];
189 *total += tr->progs_cnt[kind];
190 progs = tprogs[kind].progs;
192 hlist_for_each_entry(aux, &tr->progs_hlist[kind], tramp_hlist)
193 *progs++ = aux->prog;
198 static void __bpf_tramp_image_put_deferred(struct work_struct *work)
200 struct bpf_tramp_image *im;
202 im = container_of(work, struct bpf_tramp_image, work);
203 bpf_image_ksym_del(&im->ksym);
204 bpf_jit_free_exec(im->image);
205 bpf_jit_uncharge_modmem(1);
206 percpu_ref_exit(&im->pcref);
210 /* callback, fexit step 3 or fentry step 2 */
211 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu)
213 struct bpf_tramp_image *im;
215 im = container_of(rcu, struct bpf_tramp_image, rcu);
216 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred);
217 schedule_work(&im->work);
220 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */
221 static void __bpf_tramp_image_release(struct percpu_ref *pcref)
223 struct bpf_tramp_image *im;
225 im = container_of(pcref, struct bpf_tramp_image, pcref);
226 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
229 /* callback, fexit or fentry step 1 */
230 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu)
232 struct bpf_tramp_image *im;
234 im = container_of(rcu, struct bpf_tramp_image, rcu);
235 if (im->ip_after_call)
236 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */
237 percpu_ref_kill(&im->pcref);
239 /* the case of fentry trampoline */
240 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu);
243 static void bpf_tramp_image_put(struct bpf_tramp_image *im)
245 /* The trampoline image that calls original function is using:
246 * rcu_read_lock_trace to protect sleepable bpf progs
247 * rcu_read_lock to protect normal bpf progs
248 * percpu_ref to protect trampoline itself
249 * rcu tasks to protect trampoline asm not covered by percpu_ref
250 * (which are few asm insns before __bpf_tramp_enter and
251 * after __bpf_tramp_exit)
253 * The trampoline is unreachable before bpf_tramp_image_put().
255 * First, patch the trampoline to avoid calling into fexit progs.
256 * The progs will be freed even if the original function is still
257 * executing or sleeping.
258 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on
259 * first few asm instructions to execute and call into
260 * __bpf_tramp_enter->percpu_ref_get.
261 * Then use percpu_ref_kill to wait for the trampoline and the original
262 * function to finish.
263 * Then use call_rcu_tasks() to make sure few asm insns in
264 * the trampoline epilogue are done as well.
266 * In !PREEMPT case the task that got interrupted in the first asm
267 * insns won't go through an RCU quiescent state which the
268 * percpu_ref_kill will be waiting for. Hence the first
269 * call_rcu_tasks() is not necessary.
271 if (im->ip_after_call) {
272 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_JUMP,
273 NULL, im->ip_epilogue);
275 if (IS_ENABLED(CONFIG_PREEMPTION))
276 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
278 percpu_ref_kill(&im->pcref);
282 /* The trampoline without fexit and fmod_ret progs doesn't call original
283 * function and doesn't use percpu_ref.
284 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish.
285 * Then use call_rcu_tasks() to wait for the rest of trampoline asm
288 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
291 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, u32 idx)
293 struct bpf_tramp_image *im;
294 struct bpf_ksym *ksym;
298 im = kzalloc(sizeof(*im), GFP_KERNEL);
302 err = bpf_jit_charge_modmem(1);
307 im->image = image = bpf_jit_alloc_exec_page();
311 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL);
316 INIT_LIST_HEAD_RCU(&ksym->lnode);
317 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu_%u", key, idx);
318 bpf_image_ksym_add(image, ksym);
322 bpf_jit_free_exec(im->image);
324 bpf_jit_uncharge_modmem(1);
331 static int bpf_trampoline_update(struct bpf_trampoline *tr)
333 struct bpf_tramp_image *im;
334 struct bpf_tramp_progs *tprogs;
335 u32 flags = BPF_TRAMP_F_RESTORE_REGS;
338 tprogs = bpf_trampoline_get_progs(tr, &total);
340 return PTR_ERR(tprogs);
343 err = unregister_fentry(tr, tr->cur_image->image);
344 bpf_tramp_image_put(tr->cur_image);
345 tr->cur_image = NULL;
350 im = bpf_tramp_image_alloc(tr->key, tr->selector);
356 if (tprogs[BPF_TRAMP_FEXIT].nr_progs ||
357 tprogs[BPF_TRAMP_MODIFY_RETURN].nr_progs)
358 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
360 err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
361 &tr->func.model, flags, tprogs,
366 WARN_ON(tr->cur_image && tr->selector == 0);
367 WARN_ON(!tr->cur_image && tr->selector);
369 /* progs already running at this address */
370 err = modify_fentry(tr, tr->cur_image->image, im->image);
372 /* first time registering */
373 err = register_fentry(tr, im->image);
377 bpf_tramp_image_put(tr->cur_image);
385 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
387 switch (prog->expected_attach_type) {
388 case BPF_TRACE_FENTRY:
389 return BPF_TRAMP_FENTRY;
390 case BPF_MODIFY_RETURN:
391 return BPF_TRAMP_MODIFY_RETURN;
392 case BPF_TRACE_FEXIT:
393 return BPF_TRAMP_FEXIT;
395 if (!prog->aux->attach_func_proto->type)
396 /* The function returns void, we cannot modify its
399 return BPF_TRAMP_FEXIT;
401 return BPF_TRAMP_MODIFY_RETURN;
403 return BPF_TRAMP_REPLACE;
407 int bpf_trampoline_link_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
409 enum bpf_tramp_prog_type kind;
413 kind = bpf_attach_type_to_tramp(prog);
414 mutex_lock(&tr->mutex);
415 if (tr->extension_prog) {
416 /* cannot attach fentry/fexit if extension prog is attached.
417 * cannot overwrite extension prog either.
422 cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT];
423 if (kind == BPF_TRAMP_REPLACE) {
424 /* Cannot attach extension if fentry/fexit are in use. */
429 tr->extension_prog = prog;
430 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL,
434 if (cnt >= BPF_MAX_TRAMP_PROGS) {
438 if (!hlist_unhashed(&prog->aux->tramp_hlist)) {
439 /* prog already linked */
443 hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]);
444 tr->progs_cnt[kind]++;
445 err = bpf_trampoline_update(tr);
447 hlist_del_init(&prog->aux->tramp_hlist);
448 tr->progs_cnt[kind]--;
451 mutex_unlock(&tr->mutex);
455 /* bpf_trampoline_unlink_prog() should never fail. */
456 int bpf_trampoline_unlink_prog(struct bpf_prog *prog, struct bpf_trampoline *tr)
458 enum bpf_tramp_prog_type kind;
461 kind = bpf_attach_type_to_tramp(prog);
462 mutex_lock(&tr->mutex);
463 if (kind == BPF_TRAMP_REPLACE) {
464 WARN_ON_ONCE(!tr->extension_prog);
465 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP,
466 tr->extension_prog->bpf_func, NULL);
467 tr->extension_prog = NULL;
470 hlist_del_init(&prog->aux->tramp_hlist);
471 tr->progs_cnt[kind]--;
472 err = bpf_trampoline_update(tr);
474 mutex_unlock(&tr->mutex);
478 struct bpf_trampoline *bpf_trampoline_get(u64 key,
479 struct bpf_attach_target_info *tgt_info)
481 struct bpf_trampoline *tr;
483 tr = bpf_trampoline_lookup(key);
487 mutex_lock(&tr->mutex);
491 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel));
492 tr->func.addr = (void *)tgt_info->tgt_addr;
494 mutex_unlock(&tr->mutex);
498 void bpf_trampoline_put(struct bpf_trampoline *tr)
502 mutex_lock(&trampoline_mutex);
503 if (!refcount_dec_and_test(&tr->refcnt))
505 WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
506 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY])))
508 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
510 /* This code will be executed even when the last bpf_tramp_image
511 * is alive. All progs are detached from the trampoline and the
512 * trampoline image is patched with jmp into epilogue to skip
513 * fexit progs. The fentry-only trampoline will be freed via
514 * multiple rcu callbacks.
516 hlist_del(&tr->hlist);
519 mutex_unlock(&trampoline_mutex);
522 #define NO_START_TIME 1
523 static u64 notrace bpf_prog_start_time(void)
525 u64 start = NO_START_TIME;
527 if (static_branch_unlikely(&bpf_stats_enabled_key)) {
528 start = sched_clock();
529 if (unlikely(!start))
530 start = NO_START_TIME;
535 static void notrace inc_misses_counter(struct bpf_prog *prog)
537 struct bpf_prog_stats *stats;
539 stats = this_cpu_ptr(prog->stats);
540 u64_stats_update_begin(&stats->syncp);
542 u64_stats_update_end(&stats->syncp);
545 /* The logic is similar to BPF_PROG_RUN, but with an explicit
546 * rcu_read_lock() and migrate_disable() which are required
547 * for the trampoline. The macro is split into
548 * call __bpf_prog_enter
549 * call prog->bpf_func
550 * call __bpf_prog_exit
552 * __bpf_prog_enter returns:
553 * 0 - skip execution of the bpf prog
554 * 1 - execute bpf prog
555 * [2..MAX_U64] - excute bpf prog and record execution time.
556 * This is start time.
558 u64 notrace __bpf_prog_enter(struct bpf_prog *prog)
563 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
564 inc_misses_counter(prog);
567 return bpf_prog_start_time();
570 static void notrace update_prog_stats(struct bpf_prog *prog,
573 struct bpf_prog_stats *stats;
575 if (static_branch_unlikely(&bpf_stats_enabled_key) &&
576 /* static_key could be enabled in __bpf_prog_enter*
577 * and disabled in __bpf_prog_exit*.
579 * Hence check that 'start' is valid.
581 start > NO_START_TIME) {
582 stats = this_cpu_ptr(prog->stats);
583 u64_stats_update_begin(&stats->syncp);
585 stats->nsecs += sched_clock() - start;
586 u64_stats_update_end(&stats->syncp);
590 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
593 update_prog_stats(prog, start);
594 __this_cpu_dec(*(prog->active));
599 u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog)
601 rcu_read_lock_trace();
604 if (unlikely(__this_cpu_inc_return(*(prog->active)) != 1)) {
605 inc_misses_counter(prog);
608 return bpf_prog_start_time();
611 void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start)
613 update_prog_stats(prog, start);
614 __this_cpu_dec(*(prog->active));
616 rcu_read_unlock_trace();
619 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr)
621 percpu_ref_get(&tr->pcref);
624 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr)
626 percpu_ref_put(&tr->pcref);
630 arch_prepare_bpf_trampoline(struct bpf_tramp_image *tr, void *image, void *image_end,
631 const struct btf_func_model *m, u32 flags,
632 struct bpf_tramp_progs *tprogs,
638 static int __init init_trampolines(void)
642 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
643 INIT_HLIST_HEAD(&trampoline_table[i]);
646 late_initcall(init_trampolines);