split dev_queue
[cor.git] / kernel / bpf / trampoline.c
blob7e89f1f49d7712d6f530bba2c6572b57ae702b1f
1 // 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>
7 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */
8 #define TRAMPOLINE_HASH_BITS 10
9 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS)
11 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE];
13 /* serializes access to trampoline_table */
14 static DEFINE_MUTEX(trampoline_mutex);
16 struct bpf_trampoline *bpf_trampoline_lookup(u64 key)
18 struct bpf_trampoline *tr;
19 struct hlist_head *head;
20 void *image;
21 int i;
23 mutex_lock(&trampoline_mutex);
24 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)];
25 hlist_for_each_entry(tr, head, hlist) {
26 if (tr->key == key) {
27 refcount_inc(&tr->refcnt);
28 goto out;
31 tr = kzalloc(sizeof(*tr), GFP_KERNEL);
32 if (!tr)
33 goto out;
35 /* is_root was checked earlier. No need for bpf_jit_charge_modmem() */
36 image = bpf_jit_alloc_exec(PAGE_SIZE);
37 if (!image) {
38 kfree(tr);
39 tr = NULL;
40 goto out;
43 tr->key = key;
44 INIT_HLIST_NODE(&tr->hlist);
45 hlist_add_head(&tr->hlist, head);
46 refcount_set(&tr->refcnt, 1);
47 mutex_init(&tr->mutex);
48 for (i = 0; i < BPF_TRAMP_MAX; i++)
49 INIT_HLIST_HEAD(&tr->progs_hlist[i]);
51 set_vm_flush_reset_perms(image);
52 /* Keep image as writeable. The alternative is to keep flipping ro/rw
53 * everytime new program is attached or detached.
55 set_memory_x((long)image, 1);
56 tr->image = image;
57 out:
58 mutex_unlock(&trampoline_mutex);
59 return tr;
62 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
63 * bytes on x86. Pick a number to fit into PAGE_SIZE / 2
65 #define BPF_MAX_TRAMP_PROGS 40
67 static int bpf_trampoline_update(struct bpf_trampoline *tr)
69 void *old_image = tr->image + ((tr->selector + 1) & 1) * PAGE_SIZE/2;
70 void *new_image = tr->image + (tr->selector & 1) * PAGE_SIZE/2;
71 struct bpf_prog *progs_to_run[BPF_MAX_TRAMP_PROGS];
72 int fentry_cnt = tr->progs_cnt[BPF_TRAMP_FENTRY];
73 int fexit_cnt = tr->progs_cnt[BPF_TRAMP_FEXIT];
74 struct bpf_prog **progs, **fentry, **fexit;
75 u32 flags = BPF_TRAMP_F_RESTORE_REGS;
76 struct bpf_prog_aux *aux;
77 int err;
79 if (fentry_cnt + fexit_cnt == 0) {
80 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_CALL,
81 old_image, NULL);
82 tr->selector = 0;
83 goto out;
86 /* populate fentry progs */
87 fentry = progs = progs_to_run;
88 hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FENTRY], tramp_hlist)
89 *progs++ = aux->prog;
91 /* populate fexit progs */
92 fexit = progs;
93 hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FEXIT], tramp_hlist)
94 *progs++ = aux->prog;
96 if (fexit_cnt)
97 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
99 err = arch_prepare_bpf_trampoline(new_image, &tr->func.model, flags,
100 fentry, fentry_cnt,
101 fexit, fexit_cnt,
102 tr->func.addr);
103 if (err)
104 goto out;
106 if (tr->selector)
107 /* progs already running at this address */
108 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_CALL,
109 old_image, new_image);
110 else
111 /* first time registering */
112 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_CALL, NULL,
113 new_image);
114 if (err)
115 goto out;
116 tr->selector++;
117 out:
118 return err;
121 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(enum bpf_attach_type t)
123 switch (t) {
124 case BPF_TRACE_FENTRY:
125 return BPF_TRAMP_FENTRY;
126 default:
127 return BPF_TRAMP_FEXIT;
131 int bpf_trampoline_link_prog(struct bpf_prog *prog)
133 enum bpf_tramp_prog_type kind;
134 struct bpf_trampoline *tr;
135 int err = 0;
137 tr = prog->aux->trampoline;
138 kind = bpf_attach_type_to_tramp(prog->expected_attach_type);
139 mutex_lock(&tr->mutex);
140 if (tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT]
141 >= BPF_MAX_TRAMP_PROGS) {
142 err = -E2BIG;
143 goto out;
145 if (!hlist_unhashed(&prog->aux->tramp_hlist)) {
146 /* prog already linked */
147 err = -EBUSY;
148 goto out;
150 hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]);
151 tr->progs_cnt[kind]++;
152 err = bpf_trampoline_update(prog->aux->trampoline);
153 if (err) {
154 hlist_del(&prog->aux->tramp_hlist);
155 tr->progs_cnt[kind]--;
157 out:
158 mutex_unlock(&tr->mutex);
159 return err;
162 /* bpf_trampoline_unlink_prog() should never fail. */
163 int bpf_trampoline_unlink_prog(struct bpf_prog *prog)
165 enum bpf_tramp_prog_type kind;
166 struct bpf_trampoline *tr;
167 int err;
169 tr = prog->aux->trampoline;
170 kind = bpf_attach_type_to_tramp(prog->expected_attach_type);
171 mutex_lock(&tr->mutex);
172 hlist_del(&prog->aux->tramp_hlist);
173 tr->progs_cnt[kind]--;
174 err = bpf_trampoline_update(prog->aux->trampoline);
175 mutex_unlock(&tr->mutex);
176 return err;
179 void bpf_trampoline_put(struct bpf_trampoline *tr)
181 if (!tr)
182 return;
183 mutex_lock(&trampoline_mutex);
184 if (!refcount_dec_and_test(&tr->refcnt))
185 goto out;
186 WARN_ON_ONCE(mutex_is_locked(&tr->mutex));
187 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY])))
188 goto out;
189 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
190 goto out;
191 bpf_jit_free_exec(tr->image);
192 hlist_del(&tr->hlist);
193 kfree(tr);
194 out:
195 mutex_unlock(&trampoline_mutex);
198 /* The logic is similar to BPF_PROG_RUN, but with explicit rcu and preempt that
199 * are needed for trampoline. The macro is split into
200 * call _bpf_prog_enter
201 * call prog->bpf_func
202 * call __bpf_prog_exit
204 u64 notrace __bpf_prog_enter(void)
206 u64 start = 0;
208 rcu_read_lock();
209 preempt_disable();
210 if (static_branch_unlikely(&bpf_stats_enabled_key))
211 start = sched_clock();
212 return start;
215 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start)
217 struct bpf_prog_stats *stats;
219 if (static_branch_unlikely(&bpf_stats_enabled_key) &&
220 /* static_key could be enabled in __bpf_prog_enter
221 * and disabled in __bpf_prog_exit.
222 * And vice versa.
223 * Hence check that 'start' is not zero.
225 start) {
226 stats = this_cpu_ptr(prog->aux->stats);
227 u64_stats_update_begin(&stats->syncp);
228 stats->cnt++;
229 stats->nsecs += sched_clock() - start;
230 u64_stats_update_end(&stats->syncp);
232 preempt_enable();
233 rcu_read_unlock();
236 int __weak
237 arch_prepare_bpf_trampoline(void *image, struct btf_func_model *m, u32 flags,
238 struct bpf_prog **fentry_progs, int fentry_cnt,
239 struct bpf_prog **fexit_progs, int fexit_cnt,
240 void *orig_call)
242 return -ENOTSUPP;
245 static int __init init_trampolines(void)
247 int i;
249 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++)
250 INIT_HLIST_HEAD(&trampoline_table[i]);
251 return 0;
253 late_initcall(init_trampolines);