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> 6 #include <linux/ftrace.h> 7 8 /* dummy _ops. The verifier will operate on target program's ops. */ 9 const struct bpf_verifier_ops bpf_extension_verifier_ops = { 10 }; 11 const struct bpf_prog_ops bpf_extension_prog_ops = { 12 }; 13 14 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */ 15 #define TRAMPOLINE_HASH_BITS 10 16 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS) 17 18 static struct hlist_head trampoline_table[TRAMPOLINE_TABLE_SIZE]; 19 20 /* serializes access to trampoline_table */ 21 static DEFINE_MUTEX(trampoline_mutex); 22 23 void *bpf_jit_alloc_exec_page(void) 24 { 25 void *image; 26 27 image = bpf_jit_alloc_exec(PAGE_SIZE); 28 if (!image) 29 return NULL; 30 31 set_vm_flush_reset_perms(image); 32 /* Keep image as writeable. The alternative is to keep flipping ro/rw 33 * everytime new program is attached or detached. 34 */ 35 set_memory_x((long)image, 1); 36 return image; 37 } 38 39 struct bpf_trampoline *bpf_trampoline_lookup(u64 key) 40 { 41 struct bpf_trampoline *tr; 42 struct hlist_head *head; 43 void *image; 44 int i; 45 46 mutex_lock(&trampoline_mutex); 47 head = &trampoline_table[hash_64(key, TRAMPOLINE_HASH_BITS)]; 48 hlist_for_each_entry(tr, head, hlist) { 49 if (tr->key == key) { 50 refcount_inc(&tr->refcnt); 51 goto out; 52 } 53 } 54 tr = kzalloc(sizeof(*tr), GFP_KERNEL); 55 if (!tr) 56 goto out; 57 58 /* is_root was checked earlier. No need for bpf_jit_charge_modmem() */ 59 image = bpf_jit_alloc_exec_page(); 60 if (!image) { 61 kfree(tr); 62 tr = NULL; 63 goto out; 64 } 65 66 tr->key = key; 67 INIT_HLIST_NODE(&tr->hlist); 68 hlist_add_head(&tr->hlist, head); 69 refcount_set(&tr->refcnt, 1); 70 mutex_init(&tr->mutex); 71 for (i = 0; i < BPF_TRAMP_MAX; i++) 72 INIT_HLIST_HEAD(&tr->progs_hlist[i]); 73 tr->image = image; 74 out: 75 mutex_unlock(&trampoline_mutex); 76 return tr; 77 } 78 79 static int is_ftrace_location(void *ip) 80 { 81 long addr; 82 83 addr = ftrace_location((long)ip); 84 if (!addr) 85 return 0; 86 if (WARN_ON_ONCE(addr != (long)ip)) 87 return -EFAULT; 88 return 1; 89 } 90 91 static int unregister_fentry(struct bpf_trampoline *tr, void *old_addr) 92 { 93 void *ip = tr->func.addr; 94 int ret; 95 96 if (tr->func.ftrace_managed) 97 ret = unregister_ftrace_direct((long)ip, (long)old_addr); 98 else 99 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, NULL); 100 return ret; 101 } 102 103 static int modify_fentry(struct bpf_trampoline *tr, void *old_addr, void *new_addr) 104 { 105 void *ip = tr->func.addr; 106 int ret; 107 108 if (tr->func.ftrace_managed) 109 ret = modify_ftrace_direct((long)ip, (long)old_addr, (long)new_addr); 110 else 111 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, old_addr, new_addr); 112 return ret; 113 } 114 115 /* first time registering */ 116 static int register_fentry(struct bpf_trampoline *tr, void *new_addr) 117 { 118 void *ip = tr->func.addr; 119 int ret; 120 121 ret = is_ftrace_location(ip); 122 if (ret < 0) 123 return ret; 124 tr->func.ftrace_managed = ret; 125 126 if (tr->func.ftrace_managed) 127 ret = register_ftrace_direct((long)ip, (long)new_addr); 128 else 129 ret = bpf_arch_text_poke(ip, BPF_MOD_CALL, NULL, new_addr); 130 return ret; 131 } 132 133 /* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50 134 * bytes on x86. Pick a number to fit into PAGE_SIZE / 2 135 */ 136 #define BPF_MAX_TRAMP_PROGS 40 137 138 static int bpf_trampoline_update(struct bpf_trampoline *tr) 139 { 140 void *old_image = tr->image + ((tr->selector + 1) & 1) * PAGE_SIZE/2; 141 void *new_image = tr->image + (tr->selector & 1) * PAGE_SIZE/2; 142 struct bpf_prog *progs_to_run[BPF_MAX_TRAMP_PROGS]; 143 int fentry_cnt = tr->progs_cnt[BPF_TRAMP_FENTRY]; 144 int fexit_cnt = tr->progs_cnt[BPF_TRAMP_FEXIT]; 145 struct bpf_prog **progs, **fentry, **fexit; 146 u32 flags = BPF_TRAMP_F_RESTORE_REGS; 147 struct bpf_prog_aux *aux; 148 int err; 149 150 if (fentry_cnt + fexit_cnt == 0) { 151 err = unregister_fentry(tr, old_image); 152 tr->selector = 0; 153 goto out; 154 } 155 156 /* populate fentry progs */ 157 fentry = progs = progs_to_run; 158 hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FENTRY], tramp_hlist) 159 *progs++ = aux->prog; 160 161 /* populate fexit progs */ 162 fexit = progs; 163 hlist_for_each_entry(aux, &tr->progs_hlist[BPF_TRAMP_FEXIT], tramp_hlist) 164 *progs++ = aux->prog; 165 166 if (fexit_cnt) 167 flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 168 169 /* Though the second half of trampoline page is unused a task could be 170 * preempted in the middle of the first half of trampoline and two 171 * updates to trampoline would change the code from underneath the 172 * preempted task. Hence wait for tasks to voluntarily schedule or go 173 * to userspace. 174 */ 175 synchronize_rcu_tasks(); 176 177 err = arch_prepare_bpf_trampoline(new_image, new_image + PAGE_SIZE / 2, 178 &tr->func.model, flags, 179 fentry, fentry_cnt, 180 fexit, fexit_cnt, 181 tr->func.addr); 182 if (err < 0) 183 goto out; 184 185 if (tr->selector) 186 /* progs already running at this address */ 187 err = modify_fentry(tr, old_image, new_image); 188 else 189 /* first time registering */ 190 err = register_fentry(tr, new_image); 191 if (err) 192 goto out; 193 tr->selector++; 194 out: 195 return err; 196 } 197 198 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(enum bpf_attach_type t) 199 { 200 switch (t) { 201 case BPF_TRACE_FENTRY: 202 return BPF_TRAMP_FENTRY; 203 case BPF_TRACE_FEXIT: 204 return BPF_TRAMP_FEXIT; 205 default: 206 return BPF_TRAMP_REPLACE; 207 } 208 } 209 210 int bpf_trampoline_link_prog(struct bpf_prog *prog) 211 { 212 enum bpf_tramp_prog_type kind; 213 struct bpf_trampoline *tr; 214 int err = 0; 215 int cnt; 216 217 tr = prog->aux->trampoline; 218 kind = bpf_attach_type_to_tramp(prog->expected_attach_type); 219 mutex_lock(&tr->mutex); 220 if (tr->extension_prog) { 221 /* cannot attach fentry/fexit if extension prog is attached. 222 * cannot overwrite extension prog either. 223 */ 224 err = -EBUSY; 225 goto out; 226 } 227 cnt = tr->progs_cnt[BPF_TRAMP_FENTRY] + tr->progs_cnt[BPF_TRAMP_FEXIT]; 228 if (kind == BPF_TRAMP_REPLACE) { 229 /* Cannot attach extension if fentry/fexit are in use. */ 230 if (cnt) { 231 err = -EBUSY; 232 goto out; 233 } 234 tr->extension_prog = prog; 235 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, NULL, 236 prog->bpf_func); 237 goto out; 238 } 239 if (cnt >= BPF_MAX_TRAMP_PROGS) { 240 err = -E2BIG; 241 goto out; 242 } 243 if (!hlist_unhashed(&prog->aux->tramp_hlist)) { 244 /* prog already linked */ 245 err = -EBUSY; 246 goto out; 247 } 248 hlist_add_head(&prog->aux->tramp_hlist, &tr->progs_hlist[kind]); 249 tr->progs_cnt[kind]++; 250 err = bpf_trampoline_update(prog->aux->trampoline); 251 if (err) { 252 hlist_del(&prog->aux->tramp_hlist); 253 tr->progs_cnt[kind]--; 254 } 255 out: 256 mutex_unlock(&tr->mutex); 257 return err; 258 } 259 260 /* bpf_trampoline_unlink_prog() should never fail. */ 261 int bpf_trampoline_unlink_prog(struct bpf_prog *prog) 262 { 263 enum bpf_tramp_prog_type kind; 264 struct bpf_trampoline *tr; 265 int err; 266 267 tr = prog->aux->trampoline; 268 kind = bpf_attach_type_to_tramp(prog->expected_attach_type); 269 mutex_lock(&tr->mutex); 270 if (kind == BPF_TRAMP_REPLACE) { 271 WARN_ON_ONCE(!tr->extension_prog); 272 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, 273 tr->extension_prog->bpf_func, NULL); 274 tr->extension_prog = NULL; 275 goto out; 276 } 277 hlist_del(&prog->aux->tramp_hlist); 278 tr->progs_cnt[kind]--; 279 err = bpf_trampoline_update(prog->aux->trampoline); 280 out: 281 mutex_unlock(&tr->mutex); 282 return err; 283 } 284 285 void bpf_trampoline_put(struct bpf_trampoline *tr) 286 { 287 if (!tr) 288 return; 289 mutex_lock(&trampoline_mutex); 290 if (!refcount_dec_and_test(&tr->refcnt)) 291 goto out; 292 WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); 293 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FENTRY]))) 294 goto out; 295 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT]))) 296 goto out; 297 /* wait for tasks to get out of trampoline before freeing it */ 298 synchronize_rcu_tasks(); 299 bpf_jit_free_exec(tr->image); 300 hlist_del(&tr->hlist); 301 kfree(tr); 302 out: 303 mutex_unlock(&trampoline_mutex); 304 } 305 306 /* The logic is similar to BPF_PROG_RUN, but with explicit rcu and preempt that 307 * are needed for trampoline. The macro is split into 308 * call _bpf_prog_enter 309 * call prog->bpf_func 310 * call __bpf_prog_exit 311 */ 312 u64 notrace __bpf_prog_enter(void) 313 { 314 u64 start = 0; 315 316 rcu_read_lock(); 317 preempt_disable(); 318 if (static_branch_unlikely(&bpf_stats_enabled_key)) 319 start = sched_clock(); 320 return start; 321 } 322 323 void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start) 324 { 325 struct bpf_prog_stats *stats; 326 327 if (static_branch_unlikely(&bpf_stats_enabled_key) && 328 /* static_key could be enabled in __bpf_prog_enter 329 * and disabled in __bpf_prog_exit. 330 * And vice versa. 331 * Hence check that 'start' is not zero. 332 */ 333 start) { 334 stats = this_cpu_ptr(prog->aux->stats); 335 u64_stats_update_begin(&stats->syncp); 336 stats->cnt++; 337 stats->nsecs += sched_clock() - start; 338 u64_stats_update_end(&stats->syncp); 339 } 340 preempt_enable(); 341 rcu_read_unlock(); 342 } 343 344 int __weak 345 arch_prepare_bpf_trampoline(void *image, void *image_end, 346 const struct btf_func_model *m, u32 flags, 347 struct bpf_prog **fentry_progs, int fentry_cnt, 348 struct bpf_prog **fexit_progs, int fexit_cnt, 349 void *orig_call) 350 { 351 return -ENOTSUPP; 352 } 353 354 static int __init init_trampolines(void) 355 { 356 int i; 357 358 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 359 INIT_HLIST_HEAD(&trampoline_table[i]); 360 return 0; 361 } 362 late_initcall(init_trampolines); 363