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 #include <linux/rbtree_latch.h> 8 #include <linux/perf_event.h> 9 #include <linux/btf.h> 10 #include <linux/rcupdate_trace.h> 11 #include <linux/rcupdate_wait.h> 12 #include <linux/static_call.h> 13 #include <linux/bpf_verifier.h> 14 #include <linux/bpf_lsm.h> 15 #include <linux/delay.h> 16 17 /* dummy _ops. The verifier will operate on target program's ops. */ 18 const struct bpf_verifier_ops bpf_extension_verifier_ops = { 19 }; 20 const struct bpf_prog_ops bpf_extension_prog_ops = { 21 }; 22 23 /* btf_vmlinux has ~22k attachable functions. 1k htab is enough. */ 24 #define TRAMPOLINE_HASH_BITS 10 25 #define TRAMPOLINE_TABLE_SIZE (1 << TRAMPOLINE_HASH_BITS) 26 27 static struct hlist_head trampoline_key_table[TRAMPOLINE_TABLE_SIZE]; 28 static struct hlist_head trampoline_ip_table[TRAMPOLINE_TABLE_SIZE]; 29 30 /* serializes access to trampoline tables */ 31 static DEFINE_MUTEX(trampoline_mutex); 32 33 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 34 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex); 35 36 #ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS 37 static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip) 38 { 39 struct hlist_head *head_ip; 40 struct bpf_trampoline *tr; 41 42 mutex_lock(&trampoline_mutex); 43 head_ip = &trampoline_ip_table[hash_64(ip, TRAMPOLINE_HASH_BITS)]; 44 hlist_for_each_entry(tr, head_ip, hlist_ip) { 45 if (tr->ip == ip) 46 goto out; 47 } 48 tr = NULL; 49 out: 50 mutex_unlock(&trampoline_mutex); 51 return tr; 52 } 53 #else 54 static struct bpf_trampoline *direct_ops_ip_lookup(struct ftrace_ops *ops, unsigned long ip) 55 { 56 return ops->private; 57 } 58 #endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */ 59 60 static int bpf_tramp_ftrace_ops_func(struct ftrace_ops *ops, unsigned long ip, 61 enum ftrace_ops_cmd cmd) 62 { 63 struct bpf_trampoline *tr; 64 int ret = 0; 65 66 tr = direct_ops_ip_lookup(ops, ip); 67 if (!tr) 68 return -EINVAL; 69 70 if (cmd == FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF) { 71 /* This is called inside register_ftrace_direct_multi(), so 72 * tr->mutex is already locked. 73 */ 74 lockdep_assert_held_once(&tr->mutex); 75 76 /* Instead of updating the trampoline here, we propagate 77 * -EAGAIN to register_ftrace_direct(). Then we can 78 * retry register_ftrace_direct() after updating the 79 * trampoline. 80 */ 81 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && 82 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) { 83 if (WARN_ON_ONCE(tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY)) 84 return -EBUSY; 85 86 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; 87 return -EAGAIN; 88 } 89 90 return 0; 91 } 92 93 /* The normal locking order is 94 * tr->mutex => direct_mutex (ftrace.c) => ftrace_lock (ftrace.c) 95 * 96 * The following two commands are called from 97 * 98 * prepare_direct_functions_for_ipmodify 99 * cleanup_direct_functions_after_ipmodify 100 * 101 * In both cases, direct_mutex is already locked. Use 102 * mutex_trylock(&tr->mutex) to avoid deadlock in race condition 103 * (something else is making changes to this same trampoline). 104 */ 105 if (!mutex_trylock(&tr->mutex)) { 106 /* sleep 1 ms to make sure whatever holding tr->mutex makes 107 * some progress. 108 */ 109 msleep(1); 110 return -EAGAIN; 111 } 112 113 switch (cmd) { 114 case FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER: 115 tr->flags |= BPF_TRAMP_F_SHARE_IPMODIFY; 116 117 if ((tr->flags & BPF_TRAMP_F_CALL_ORIG) && 118 !(tr->flags & BPF_TRAMP_F_ORIG_STACK)) 119 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */); 120 break; 121 case FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER: 122 tr->flags &= ~BPF_TRAMP_F_SHARE_IPMODIFY; 123 124 if (tr->flags & BPF_TRAMP_F_ORIG_STACK) 125 ret = bpf_trampoline_update(tr, false /* lock_direct_mutex */); 126 break; 127 default: 128 ret = -EINVAL; 129 break; 130 } 131 132 mutex_unlock(&tr->mutex); 133 return ret; 134 } 135 #endif 136 137 bool bpf_prog_has_trampoline(const struct bpf_prog *prog) 138 { 139 enum bpf_attach_type eatype = prog->expected_attach_type; 140 enum bpf_prog_type ptype = prog->type; 141 142 switch (ptype) { 143 case BPF_PROG_TYPE_TRACING: 144 if (eatype == BPF_TRACE_FENTRY || eatype == BPF_TRACE_FEXIT || 145 eatype == BPF_MODIFY_RETURN || eatype == BPF_TRACE_FSESSION) 146 return true; 147 return false; 148 case BPF_PROG_TYPE_LSM: 149 return eatype == BPF_LSM_MAC; 150 default: 151 return false; 152 } 153 } 154 155 void bpf_image_ksym_init(void *data, unsigned int size, struct bpf_ksym *ksym) 156 { 157 ksym->start = (unsigned long) data; 158 ksym->end = ksym->start + size; 159 } 160 161 void bpf_image_ksym_add(struct bpf_ksym *ksym) 162 { 163 bpf_ksym_add(ksym); 164 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 165 PAGE_SIZE, false, ksym->name); 166 } 167 168 void bpf_image_ksym_del(struct bpf_ksym *ksym) 169 { 170 bpf_ksym_del(ksym); 171 perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start, 172 PAGE_SIZE, true, ksym->name); 173 } 174 175 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 176 #ifdef CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS 177 /* 178 * We have only single direct_ops which contains all the direct call 179 * sites and is the only global ftrace_ops for all trampolines. 180 * 181 * We use 'update_ftrace_direct_*' api for attachment. 182 */ 183 struct ftrace_ops direct_ops = { 184 .ops_func = bpf_tramp_ftrace_ops_func, 185 }; 186 187 static int direct_ops_alloc(struct bpf_trampoline *tr) 188 { 189 tr->fops = &direct_ops; 190 return 0; 191 } 192 193 static void direct_ops_free(struct bpf_trampoline *tr) { } 194 195 static struct ftrace_hash *hash_from_ip(struct bpf_trampoline *tr, void *ptr) 196 { 197 unsigned long ip, addr = (unsigned long) ptr; 198 struct ftrace_hash *hash; 199 200 ip = ftrace_location(tr->ip); 201 if (!ip) 202 return NULL; 203 hash = alloc_ftrace_hash(FTRACE_HASH_DEFAULT_BITS); 204 if (!hash) 205 return NULL; 206 if (bpf_trampoline_use_jmp(tr->flags)) 207 addr = ftrace_jmp_set(addr); 208 if (!add_ftrace_hash_entry_direct(hash, ip, addr)) { 209 free_ftrace_hash(hash); 210 return NULL; 211 } 212 return hash; 213 } 214 215 static int direct_ops_add(struct bpf_trampoline *tr, void *addr) 216 { 217 struct ftrace_hash *hash = hash_from_ip(tr, addr); 218 int err; 219 220 if (!hash) 221 return -ENOMEM; 222 err = update_ftrace_direct_add(tr->fops, hash); 223 free_ftrace_hash(hash); 224 return err; 225 } 226 227 static int direct_ops_del(struct bpf_trampoline *tr, void *addr) 228 { 229 struct ftrace_hash *hash = hash_from_ip(tr, addr); 230 int err; 231 232 if (!hash) 233 return -ENOMEM; 234 err = update_ftrace_direct_del(tr->fops, hash); 235 free_ftrace_hash(hash); 236 return err; 237 } 238 239 static int direct_ops_mod(struct bpf_trampoline *tr, void *addr, bool lock_direct_mutex) 240 { 241 struct ftrace_hash *hash = hash_from_ip(tr, addr); 242 int err; 243 244 if (!hash) 245 return -ENOMEM; 246 err = update_ftrace_direct_mod(tr->fops, hash, lock_direct_mutex); 247 free_ftrace_hash(hash); 248 return err; 249 } 250 #else 251 /* 252 * We allocate ftrace_ops object for each trampoline and it contains 253 * call site specific for that trampoline. 254 * 255 * We use *_ftrace_direct api for attachment. 256 */ 257 static int direct_ops_alloc(struct bpf_trampoline *tr) 258 { 259 tr->fops = kzalloc_obj(struct ftrace_ops); 260 if (!tr->fops) 261 return -ENOMEM; 262 tr->fops->private = tr; 263 tr->fops->ops_func = bpf_tramp_ftrace_ops_func; 264 return 0; 265 } 266 267 static void direct_ops_free(struct bpf_trampoline *tr) 268 { 269 if (!tr->fops) 270 return; 271 ftrace_free_filter(tr->fops); 272 kfree(tr->fops); 273 } 274 275 static int direct_ops_add(struct bpf_trampoline *tr, void *ptr) 276 { 277 unsigned long addr = (unsigned long) ptr; 278 struct ftrace_ops *ops = tr->fops; 279 int ret; 280 281 if (bpf_trampoline_use_jmp(tr->flags)) 282 addr = ftrace_jmp_set(addr); 283 284 ret = ftrace_set_filter_ip(ops, tr->ip, 0, 1); 285 if (ret) 286 return ret; 287 return register_ftrace_direct(ops, addr); 288 } 289 290 static int direct_ops_del(struct bpf_trampoline *tr, void *addr) 291 { 292 return unregister_ftrace_direct(tr->fops, (long)addr, false); 293 } 294 295 static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex) 296 { 297 unsigned long addr = (unsigned long) ptr; 298 struct ftrace_ops *ops = tr->fops; 299 300 if (bpf_trampoline_use_jmp(tr->flags)) 301 addr = ftrace_jmp_set(addr); 302 if (lock_direct_mutex) 303 return modify_ftrace_direct(ops, addr); 304 return modify_ftrace_direct_nolock(ops, addr); 305 } 306 #endif /* CONFIG_HAVE_SINGLE_FTRACE_DIRECT_OPS */ 307 #else 308 static void direct_ops_free(struct bpf_trampoline *tr) { } 309 310 static int direct_ops_alloc(struct bpf_trampoline *tr) 311 { 312 return 0; 313 } 314 315 static int direct_ops_add(struct bpf_trampoline *tr, void *addr) 316 { 317 return -ENODEV; 318 } 319 320 static int direct_ops_del(struct bpf_trampoline *tr, void *addr) 321 { 322 return -ENODEV; 323 } 324 325 static int direct_ops_mod(struct bpf_trampoline *tr, void *ptr, bool lock_direct_mutex) 326 { 327 return -ENODEV; 328 } 329 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */ 330 331 static struct bpf_trampoline *bpf_trampoline_lookup(u64 key, unsigned long ip) 332 { 333 struct bpf_trampoline *tr; 334 struct hlist_head *head; 335 int i; 336 337 mutex_lock(&trampoline_mutex); 338 head = &trampoline_key_table[hash_64(key, TRAMPOLINE_HASH_BITS)]; 339 hlist_for_each_entry(tr, head, hlist_key) { 340 if (tr->key == key) { 341 refcount_inc(&tr->refcnt); 342 goto out; 343 } 344 } 345 tr = kzalloc_obj(*tr); 346 if (!tr) 347 goto out; 348 if (direct_ops_alloc(tr)) { 349 kfree(tr); 350 tr = NULL; 351 goto out; 352 } 353 354 tr->key = key; 355 tr->ip = ftrace_location(ip); 356 INIT_HLIST_NODE(&tr->hlist_key); 357 INIT_HLIST_NODE(&tr->hlist_ip); 358 hlist_add_head(&tr->hlist_key, head); 359 head = &trampoline_ip_table[hash_64(tr->ip, TRAMPOLINE_HASH_BITS)]; 360 hlist_add_head(&tr->hlist_ip, head); 361 refcount_set(&tr->refcnt, 1); 362 mutex_init(&tr->mutex); 363 for (i = 0; i < BPF_TRAMP_MAX; i++) 364 INIT_HLIST_HEAD(&tr->progs_hlist[i]); 365 out: 366 mutex_unlock(&trampoline_mutex); 367 return tr; 368 } 369 370 static int bpf_trampoline_update_fentry(struct bpf_trampoline *tr, u32 orig_flags, 371 void *old_addr, void *new_addr) 372 { 373 enum bpf_text_poke_type new_t = BPF_MOD_CALL, old_t = BPF_MOD_CALL; 374 void *ip = tr->func.addr; 375 376 if (!new_addr) 377 new_t = BPF_MOD_NOP; 378 else if (bpf_trampoline_use_jmp(tr->flags)) 379 new_t = BPF_MOD_JUMP; 380 381 if (!old_addr) 382 old_t = BPF_MOD_NOP; 383 else if (bpf_trampoline_use_jmp(orig_flags)) 384 old_t = BPF_MOD_JUMP; 385 386 return bpf_arch_text_poke(ip, old_t, new_t, old_addr, new_addr); 387 } 388 389 static int unregister_fentry(struct bpf_trampoline *tr, u32 orig_flags, 390 void *old_addr) 391 { 392 int ret; 393 394 if (tr->func.ftrace_managed) 395 ret = direct_ops_del(tr, old_addr); 396 else 397 ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, NULL); 398 399 return ret; 400 } 401 402 static int modify_fentry(struct bpf_trampoline *tr, u32 orig_flags, 403 void *old_addr, void *new_addr, 404 bool lock_direct_mutex) 405 { 406 int ret; 407 408 if (tr->func.ftrace_managed) { 409 ret = direct_ops_mod(tr, new_addr, lock_direct_mutex); 410 } else { 411 ret = bpf_trampoline_update_fentry(tr, orig_flags, old_addr, 412 new_addr); 413 } 414 return ret; 415 } 416 417 /* first time registering */ 418 static int register_fentry(struct bpf_trampoline *tr, void *new_addr) 419 { 420 void *ip = tr->func.addr; 421 unsigned long faddr; 422 int ret; 423 424 faddr = ftrace_location((unsigned long)ip); 425 if (faddr) { 426 if (!tr->fops) 427 return -ENOTSUPP; 428 tr->func.ftrace_managed = true; 429 } 430 431 if (tr->func.ftrace_managed) { 432 ret = direct_ops_add(tr, new_addr); 433 } else { 434 ret = bpf_trampoline_update_fentry(tr, 0, NULL, new_addr); 435 } 436 437 return ret; 438 } 439 440 static struct bpf_tramp_links * 441 bpf_trampoline_get_progs(const struct bpf_trampoline *tr, int *total, bool *ip_arg) 442 { 443 struct bpf_tramp_link *link; 444 struct bpf_tramp_links *tlinks; 445 struct bpf_tramp_link **links; 446 int kind; 447 448 *total = 0; 449 tlinks = kzalloc_objs(*tlinks, BPF_TRAMP_MAX); 450 if (!tlinks) 451 return ERR_PTR(-ENOMEM); 452 453 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 454 tlinks[kind].nr_links = tr->progs_cnt[kind]; 455 *total += tr->progs_cnt[kind]; 456 links = tlinks[kind].links; 457 458 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) { 459 *ip_arg |= link->link.prog->call_get_func_ip; 460 *links++ = link; 461 } 462 } 463 return tlinks; 464 } 465 466 static void bpf_tramp_image_free(struct bpf_tramp_image *im) 467 { 468 bpf_image_ksym_del(&im->ksym); 469 arch_free_bpf_trampoline(im->image, im->size); 470 bpf_jit_uncharge_modmem(im->size); 471 percpu_ref_exit(&im->pcref); 472 kfree_rcu(im, rcu); 473 } 474 475 static void __bpf_tramp_image_put_deferred(struct work_struct *work) 476 { 477 struct bpf_tramp_image *im; 478 479 im = container_of(work, struct bpf_tramp_image, work); 480 bpf_tramp_image_free(im); 481 } 482 483 /* callback, fexit step 3 or fentry step 2 */ 484 static void __bpf_tramp_image_put_rcu(struct rcu_head *rcu) 485 { 486 struct bpf_tramp_image *im; 487 488 im = container_of(rcu, struct bpf_tramp_image, rcu); 489 INIT_WORK(&im->work, __bpf_tramp_image_put_deferred); 490 schedule_work(&im->work); 491 } 492 493 /* callback, fexit step 2. Called after percpu_ref_kill confirms. */ 494 static void __bpf_tramp_image_release(struct percpu_ref *pcref) 495 { 496 struct bpf_tramp_image *im; 497 498 im = container_of(pcref, struct bpf_tramp_image, pcref); 499 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 500 } 501 502 /* callback, fexit or fentry step 1 */ 503 static void __bpf_tramp_image_put_rcu_tasks(struct rcu_head *rcu) 504 { 505 struct bpf_tramp_image *im; 506 507 im = container_of(rcu, struct bpf_tramp_image, rcu); 508 if (im->ip_after_call) 509 /* the case of fmod_ret/fexit trampoline and CONFIG_PREEMPTION=y */ 510 percpu_ref_kill(&im->pcref); 511 else 512 /* the case of fentry trampoline */ 513 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu); 514 } 515 516 static void bpf_tramp_image_put(struct bpf_tramp_image *im) 517 { 518 /* The trampoline image that calls original function is using: 519 * rcu_read_lock_trace to protect sleepable bpf progs 520 * rcu_read_lock to protect normal bpf progs 521 * percpu_ref to protect trampoline itself 522 * rcu tasks to protect trampoline asm not covered by percpu_ref 523 * (which are few asm insns before __bpf_tramp_enter and 524 * after __bpf_tramp_exit) 525 * 526 * The trampoline is unreachable before bpf_tramp_image_put(). 527 * 528 * First, patch the trampoline to avoid calling into fexit progs. 529 * The progs will be freed even if the original function is still 530 * executing or sleeping. 531 * In case of CONFIG_PREEMPT=y use call_rcu_tasks() to wait on 532 * first few asm instructions to execute and call into 533 * __bpf_tramp_enter->percpu_ref_get. 534 * Then use percpu_ref_kill to wait for the trampoline and the original 535 * function to finish. 536 * Then use call_rcu_tasks() to make sure few asm insns in 537 * the trampoline epilogue are done as well. 538 * 539 * In !PREEMPT case the task that got interrupted in the first asm 540 * insns won't go through an RCU quiescent state which the 541 * percpu_ref_kill will be waiting for. Hence the first 542 * call_rcu_tasks() is not necessary. 543 */ 544 if (im->ip_after_call) { 545 int err = bpf_arch_text_poke(im->ip_after_call, BPF_MOD_NOP, 546 BPF_MOD_JUMP, NULL, 547 im->ip_epilogue); 548 WARN_ON(err); 549 if (IS_ENABLED(CONFIG_TASKS_RCU)) 550 call_rcu_tasks(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 551 else 552 percpu_ref_kill(&im->pcref); 553 return; 554 } 555 556 /* The trampoline without fexit and fmod_ret progs doesn't call original 557 * function and doesn't use percpu_ref. 558 * Use call_rcu_tasks_trace() to wait for sleepable progs to finish. 559 * Then use call_rcu_tasks() to wait for the rest of trampoline asm 560 * and normal progs. 561 */ 562 call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks); 563 } 564 565 static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size) 566 { 567 struct bpf_tramp_image *im; 568 struct bpf_ksym *ksym; 569 void *image; 570 int err = -ENOMEM; 571 572 im = kzalloc_obj(*im); 573 if (!im) 574 goto out; 575 576 err = bpf_jit_charge_modmem(size); 577 if (err) 578 goto out_free_im; 579 im->size = size; 580 581 err = -ENOMEM; 582 im->image = image = arch_alloc_bpf_trampoline(size); 583 if (!image) 584 goto out_uncharge; 585 586 err = percpu_ref_init(&im->pcref, __bpf_tramp_image_release, 0, GFP_KERNEL); 587 if (err) 588 goto out_free_image; 589 590 ksym = &im->ksym; 591 INIT_LIST_HEAD_RCU(&ksym->lnode); 592 snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key); 593 bpf_image_ksym_init(image, size, ksym); 594 bpf_image_ksym_add(ksym); 595 return im; 596 597 out_free_image: 598 arch_free_bpf_trampoline(im->image, im->size); 599 out_uncharge: 600 bpf_jit_uncharge_modmem(size); 601 out_free_im: 602 kfree(im); 603 out: 604 return ERR_PTR(err); 605 } 606 607 static int bpf_trampoline_update(struct bpf_trampoline *tr, bool lock_direct_mutex) 608 { 609 struct bpf_tramp_image *im; 610 struct bpf_tramp_links *tlinks; 611 u32 orig_flags = tr->flags; 612 bool ip_arg = false; 613 int err, total, size; 614 615 tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg); 616 if (IS_ERR(tlinks)) 617 return PTR_ERR(tlinks); 618 619 if (total == 0) { 620 err = unregister_fentry(tr, orig_flags, tr->cur_image->image); 621 bpf_tramp_image_put(tr->cur_image); 622 tr->cur_image = NULL; 623 goto out; 624 } 625 626 /* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */ 627 tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX); 628 629 if (tlinks[BPF_TRAMP_FEXIT].nr_links || 630 tlinks[BPF_TRAMP_MODIFY_RETURN].nr_links) { 631 /* NOTE: BPF_TRAMP_F_RESTORE_REGS and BPF_TRAMP_F_SKIP_FRAME 632 * should not be set together. 633 */ 634 tr->flags |= BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME; 635 } else { 636 tr->flags |= BPF_TRAMP_F_RESTORE_REGS; 637 } 638 639 if (ip_arg) 640 tr->flags |= BPF_TRAMP_F_IP_ARG; 641 642 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 643 again: 644 if (tr->flags & BPF_TRAMP_F_CALL_ORIG) { 645 if (tr->flags & BPF_TRAMP_F_SHARE_IPMODIFY) { 646 /* The BPF_TRAMP_F_SKIP_FRAME can be cleared in the 647 * first try, reset it in the second try. 648 */ 649 tr->flags |= BPF_TRAMP_F_ORIG_STACK | BPF_TRAMP_F_SKIP_FRAME; 650 } else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_JMP)) { 651 /* Use "jmp" instead of "call" for the trampoline 652 * in the origin call case, and we don't need to 653 * skip the frame. 654 */ 655 tr->flags &= ~BPF_TRAMP_F_SKIP_FRAME; 656 } 657 } 658 #endif 659 660 size = arch_bpf_trampoline_size(&tr->func.model, tr->flags, 661 tlinks, tr->func.addr); 662 if (size < 0) { 663 err = size; 664 goto out; 665 } 666 667 if (size > PAGE_SIZE) { 668 err = -E2BIG; 669 goto out; 670 } 671 672 im = bpf_tramp_image_alloc(tr->key, size); 673 if (IS_ERR(im)) { 674 err = PTR_ERR(im); 675 goto out; 676 } 677 678 err = arch_prepare_bpf_trampoline(im, im->image, im->image + size, 679 &tr->func.model, tr->flags, tlinks, 680 tr->func.addr); 681 if (err < 0) 682 goto out_free; 683 684 err = arch_protect_bpf_trampoline(im->image, im->size); 685 if (err) 686 goto out_free; 687 688 if (tr->cur_image) 689 /* progs already running at this address */ 690 err = modify_fentry(tr, orig_flags, tr->cur_image->image, 691 im->image, lock_direct_mutex); 692 else 693 /* first time registering */ 694 err = register_fentry(tr, im->image); 695 696 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS 697 if (err == -EAGAIN) { 698 /* -EAGAIN from bpf_tramp_ftrace_ops_func. Now 699 * BPF_TRAMP_F_SHARE_IPMODIFY is set, we can generate the 700 * trampoline again, and retry register. 701 */ 702 bpf_tramp_image_free(im); 703 goto again; 704 } 705 #endif 706 if (err) 707 goto out_free; 708 709 if (tr->cur_image) 710 bpf_tramp_image_put(tr->cur_image); 711 tr->cur_image = im; 712 out: 713 /* If any error happens, restore previous flags */ 714 if (err) 715 tr->flags = orig_flags; 716 kfree(tlinks); 717 return err; 718 719 out_free: 720 bpf_tramp_image_free(im); 721 goto out; 722 } 723 724 static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog) 725 { 726 switch (prog->expected_attach_type) { 727 case BPF_TRACE_FENTRY: 728 return BPF_TRAMP_FENTRY; 729 case BPF_MODIFY_RETURN: 730 return BPF_TRAMP_MODIFY_RETURN; 731 case BPF_TRACE_FEXIT: 732 return BPF_TRAMP_FEXIT; 733 case BPF_TRACE_FSESSION: 734 return BPF_TRAMP_FSESSION; 735 case BPF_LSM_MAC: 736 if (!prog->aux->attach_func_proto->type) 737 /* The function returns void, we cannot modify its 738 * return value. 739 */ 740 return BPF_TRAMP_FEXIT; 741 else 742 return BPF_TRAMP_MODIFY_RETURN; 743 default: 744 return BPF_TRAMP_REPLACE; 745 } 746 } 747 748 static int bpf_freplace_check_tgt_prog(struct bpf_prog *tgt_prog) 749 { 750 struct bpf_prog_aux *aux = tgt_prog->aux; 751 752 guard(mutex)(&aux->ext_mutex); 753 if (aux->prog_array_member_cnt) 754 /* Program extensions can not extend target prog when the target 755 * prog has been updated to any prog_array map as tail callee. 756 * It's to prevent a potential infinite loop like: 757 * tgt prog entry -> tgt prog subprog -> freplace prog entry 758 * --tailcall-> tgt prog entry. 759 */ 760 return -EBUSY; 761 762 aux->is_extended = true; 763 return 0; 764 } 765 766 static int __bpf_trampoline_link_prog(struct bpf_tramp_link *link, 767 struct bpf_trampoline *tr, 768 struct bpf_prog *tgt_prog) 769 { 770 struct bpf_fsession_link *fslink = NULL; 771 enum bpf_tramp_prog_type kind; 772 struct bpf_tramp_link *link_exiting; 773 struct hlist_head *prog_list; 774 int err = 0; 775 int cnt = 0, i; 776 777 kind = bpf_attach_type_to_tramp(link->link.prog); 778 if (tr->extension_prog) 779 /* cannot attach fentry/fexit if extension prog is attached. 780 * cannot overwrite extension prog either. 781 */ 782 return -EBUSY; 783 784 for (i = 0; i < BPF_TRAMP_MAX; i++) 785 cnt += tr->progs_cnt[i]; 786 787 if (kind == BPF_TRAMP_REPLACE) { 788 /* Cannot attach extension if fentry/fexit are in use. */ 789 if (cnt) 790 return -EBUSY; 791 err = bpf_freplace_check_tgt_prog(tgt_prog); 792 if (err) 793 return err; 794 tr->extension_prog = link->link.prog; 795 return bpf_arch_text_poke(tr->func.addr, BPF_MOD_NOP, 796 BPF_MOD_JUMP, NULL, 797 link->link.prog->bpf_func); 798 } 799 if (kind == BPF_TRAMP_FSESSION) { 800 prog_list = &tr->progs_hlist[BPF_TRAMP_FENTRY]; 801 cnt++; 802 } else { 803 prog_list = &tr->progs_hlist[kind]; 804 } 805 if (cnt >= BPF_MAX_TRAMP_LINKS) 806 return -E2BIG; 807 if (!hlist_unhashed(&link->tramp_hlist)) 808 /* prog already linked */ 809 return -EBUSY; 810 hlist_for_each_entry(link_exiting, prog_list, tramp_hlist) { 811 if (link_exiting->link.prog != link->link.prog) 812 continue; 813 /* prog already linked */ 814 return -EBUSY; 815 } 816 817 hlist_add_head(&link->tramp_hlist, prog_list); 818 if (kind == BPF_TRAMP_FSESSION) { 819 tr->progs_cnt[BPF_TRAMP_FENTRY]++; 820 fslink = container_of(link, struct bpf_fsession_link, link.link); 821 hlist_add_head(&fslink->fexit.tramp_hlist, &tr->progs_hlist[BPF_TRAMP_FEXIT]); 822 tr->progs_cnt[BPF_TRAMP_FEXIT]++; 823 } else { 824 tr->progs_cnt[kind]++; 825 } 826 err = bpf_trampoline_update(tr, true /* lock_direct_mutex */); 827 if (err) { 828 hlist_del_init(&link->tramp_hlist); 829 if (kind == BPF_TRAMP_FSESSION) { 830 tr->progs_cnt[BPF_TRAMP_FENTRY]--; 831 hlist_del_init(&fslink->fexit.tramp_hlist); 832 tr->progs_cnt[BPF_TRAMP_FEXIT]--; 833 } else { 834 tr->progs_cnt[kind]--; 835 } 836 } 837 return err; 838 } 839 840 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, 841 struct bpf_trampoline *tr, 842 struct bpf_prog *tgt_prog) 843 { 844 int err; 845 846 mutex_lock(&tr->mutex); 847 err = __bpf_trampoline_link_prog(link, tr, tgt_prog); 848 mutex_unlock(&tr->mutex); 849 return err; 850 } 851 852 static int __bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, 853 struct bpf_trampoline *tr, 854 struct bpf_prog *tgt_prog) 855 { 856 enum bpf_tramp_prog_type kind; 857 int err; 858 859 kind = bpf_attach_type_to_tramp(link->link.prog); 860 if (kind == BPF_TRAMP_REPLACE) { 861 WARN_ON_ONCE(!tr->extension_prog); 862 err = bpf_arch_text_poke(tr->func.addr, BPF_MOD_JUMP, 863 BPF_MOD_NOP, 864 tr->extension_prog->bpf_func, NULL); 865 tr->extension_prog = NULL; 866 guard(mutex)(&tgt_prog->aux->ext_mutex); 867 tgt_prog->aux->is_extended = false; 868 return err; 869 } else if (kind == BPF_TRAMP_FSESSION) { 870 struct bpf_fsession_link *fslink = 871 container_of(link, struct bpf_fsession_link, link.link); 872 873 hlist_del_init(&fslink->fexit.tramp_hlist); 874 tr->progs_cnt[BPF_TRAMP_FEXIT]--; 875 kind = BPF_TRAMP_FENTRY; 876 } 877 hlist_del_init(&link->tramp_hlist); 878 tr->progs_cnt[kind]--; 879 return bpf_trampoline_update(tr, true /* lock_direct_mutex */); 880 } 881 882 /* bpf_trampoline_unlink_prog() should never fail. */ 883 int bpf_trampoline_unlink_prog(struct bpf_tramp_link *link, 884 struct bpf_trampoline *tr, 885 struct bpf_prog *tgt_prog) 886 { 887 int err; 888 889 mutex_lock(&tr->mutex); 890 err = __bpf_trampoline_unlink_prog(link, tr, tgt_prog); 891 mutex_unlock(&tr->mutex); 892 return err; 893 } 894 895 #if defined(CONFIG_CGROUP_BPF) && defined(CONFIG_BPF_LSM) 896 static void bpf_shim_tramp_link_release(struct bpf_link *link) 897 { 898 struct bpf_shim_tramp_link *shim_link = 899 container_of(link, struct bpf_shim_tramp_link, link.link); 900 901 /* paired with 'shim_link->trampoline = tr' in bpf_trampoline_link_cgroup_shim */ 902 if (!shim_link->trampoline) 903 return; 904 905 WARN_ON_ONCE(bpf_trampoline_unlink_prog(&shim_link->link, shim_link->trampoline, NULL)); 906 bpf_trampoline_put(shim_link->trampoline); 907 } 908 909 static void bpf_shim_tramp_link_dealloc(struct bpf_link *link) 910 { 911 struct bpf_shim_tramp_link *shim_link = 912 container_of(link, struct bpf_shim_tramp_link, link.link); 913 914 kfree(shim_link); 915 } 916 917 static const struct bpf_link_ops bpf_shim_tramp_link_lops = { 918 .release = bpf_shim_tramp_link_release, 919 .dealloc = bpf_shim_tramp_link_dealloc, 920 }; 921 922 static struct bpf_shim_tramp_link *cgroup_shim_alloc(const struct bpf_prog *prog, 923 bpf_func_t bpf_func, 924 int cgroup_atype, 925 enum bpf_attach_type attach_type) 926 { 927 struct bpf_shim_tramp_link *shim_link = NULL; 928 struct bpf_prog *p; 929 930 shim_link = kzalloc_obj(*shim_link, GFP_USER); 931 if (!shim_link) 932 return NULL; 933 934 p = bpf_prog_alloc(1, 0); 935 if (!p) { 936 kfree(shim_link); 937 return NULL; 938 } 939 940 p->jited = false; 941 p->bpf_func = bpf_func; 942 943 p->aux->cgroup_atype = cgroup_atype; 944 p->aux->attach_func_proto = prog->aux->attach_func_proto; 945 p->aux->attach_btf_id = prog->aux->attach_btf_id; 946 p->aux->attach_btf = prog->aux->attach_btf; 947 btf_get(p->aux->attach_btf); 948 p->type = BPF_PROG_TYPE_LSM; 949 p->expected_attach_type = BPF_LSM_MAC; 950 bpf_prog_inc(p); 951 bpf_link_init(&shim_link->link.link, BPF_LINK_TYPE_UNSPEC, 952 &bpf_shim_tramp_link_lops, p, attach_type); 953 bpf_cgroup_atype_get(p->aux->attach_btf_id, cgroup_atype); 954 955 return shim_link; 956 } 957 958 static struct bpf_shim_tramp_link *cgroup_shim_find(struct bpf_trampoline *tr, 959 bpf_func_t bpf_func) 960 { 961 struct bpf_tramp_link *link; 962 int kind; 963 964 for (kind = 0; kind < BPF_TRAMP_MAX; kind++) { 965 hlist_for_each_entry(link, &tr->progs_hlist[kind], tramp_hlist) { 966 struct bpf_prog *p = link->link.prog; 967 968 if (p->bpf_func == bpf_func) 969 return container_of(link, struct bpf_shim_tramp_link, link); 970 } 971 } 972 973 return NULL; 974 } 975 976 int bpf_trampoline_link_cgroup_shim(struct bpf_prog *prog, 977 int cgroup_atype, 978 enum bpf_attach_type attach_type) 979 { 980 struct bpf_shim_tramp_link *shim_link = NULL; 981 struct bpf_attach_target_info tgt_info = {}; 982 struct bpf_trampoline *tr; 983 bpf_func_t bpf_func; 984 u64 key; 985 int err; 986 987 err = bpf_check_attach_target(NULL, prog, NULL, 988 prog->aux->attach_btf_id, 989 &tgt_info); 990 if (err) 991 return err; 992 993 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 994 prog->aux->attach_btf_id); 995 996 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 997 tr = bpf_trampoline_get(key, &tgt_info); 998 if (!tr) 999 return -ENOMEM; 1000 1001 mutex_lock(&tr->mutex); 1002 1003 shim_link = cgroup_shim_find(tr, bpf_func); 1004 if (shim_link && !IS_ERR(bpf_link_inc_not_zero(&shim_link->link.link))) { 1005 /* Reusing existing shim attached by the other program. */ 1006 mutex_unlock(&tr->mutex); 1007 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 1008 return 0; 1009 } 1010 1011 /* Allocate and install new shim. */ 1012 1013 shim_link = cgroup_shim_alloc(prog, bpf_func, cgroup_atype, attach_type); 1014 if (!shim_link) { 1015 err = -ENOMEM; 1016 goto err; 1017 } 1018 1019 err = __bpf_trampoline_link_prog(&shim_link->link, tr, NULL); 1020 if (err) 1021 goto err; 1022 1023 shim_link->trampoline = tr; 1024 /* note, we're still holding tr refcnt from above */ 1025 1026 mutex_unlock(&tr->mutex); 1027 1028 return 0; 1029 err: 1030 mutex_unlock(&tr->mutex); 1031 1032 if (shim_link) 1033 bpf_link_put(&shim_link->link.link); 1034 1035 /* have to release tr while _not_ holding its mutex */ 1036 bpf_trampoline_put(tr); /* bpf_trampoline_get above */ 1037 1038 return err; 1039 } 1040 1041 void bpf_trampoline_unlink_cgroup_shim(struct bpf_prog *prog) 1042 { 1043 struct bpf_shim_tramp_link *shim_link = NULL; 1044 struct bpf_trampoline *tr; 1045 bpf_func_t bpf_func; 1046 u64 key; 1047 1048 key = bpf_trampoline_compute_key(NULL, prog->aux->attach_btf, 1049 prog->aux->attach_btf_id); 1050 1051 bpf_lsm_find_cgroup_shim(prog, &bpf_func); 1052 tr = bpf_trampoline_lookup(key, 0); 1053 if (WARN_ON_ONCE(!tr)) 1054 return; 1055 1056 mutex_lock(&tr->mutex); 1057 shim_link = cgroup_shim_find(tr, bpf_func); 1058 mutex_unlock(&tr->mutex); 1059 1060 if (shim_link) 1061 bpf_link_put(&shim_link->link.link); 1062 1063 bpf_trampoline_put(tr); /* bpf_trampoline_lookup above */ 1064 } 1065 #endif 1066 1067 struct bpf_trampoline *bpf_trampoline_get(u64 key, 1068 struct bpf_attach_target_info *tgt_info) 1069 { 1070 struct bpf_trampoline *tr; 1071 1072 tr = bpf_trampoline_lookup(key, tgt_info->tgt_addr); 1073 if (!tr) 1074 return NULL; 1075 1076 mutex_lock(&tr->mutex); 1077 if (tr->func.addr) 1078 goto out; 1079 1080 memcpy(&tr->func.model, &tgt_info->fmodel, sizeof(tgt_info->fmodel)); 1081 tr->func.addr = (void *)tgt_info->tgt_addr; 1082 out: 1083 mutex_unlock(&tr->mutex); 1084 return tr; 1085 } 1086 1087 void bpf_trampoline_put(struct bpf_trampoline *tr) 1088 { 1089 int i; 1090 1091 if (!tr) 1092 return; 1093 mutex_lock(&trampoline_mutex); 1094 if (!refcount_dec_and_test(&tr->refcnt)) 1095 goto out; 1096 WARN_ON_ONCE(mutex_is_locked(&tr->mutex)); 1097 1098 for (i = 0; i < BPF_TRAMP_MAX; i++) 1099 if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[i]))) 1100 goto out; 1101 1102 /* This code will be executed even when the last bpf_tramp_image 1103 * is alive. All progs are detached from the trampoline and the 1104 * trampoline image is patched with jmp into epilogue to skip 1105 * fexit progs. The fentry-only trampoline will be freed via 1106 * multiple rcu callbacks. 1107 */ 1108 hlist_del(&tr->hlist_key); 1109 hlist_del(&tr->hlist_ip); 1110 direct_ops_free(tr); 1111 kfree(tr); 1112 out: 1113 mutex_unlock(&trampoline_mutex); 1114 } 1115 1116 #define NO_START_TIME 1 1117 static __always_inline u64 notrace bpf_prog_start_time(void) 1118 { 1119 u64 start = NO_START_TIME; 1120 1121 if (static_branch_unlikely(&bpf_stats_enabled_key)) { 1122 start = sched_clock(); 1123 if (unlikely(!start)) 1124 start = NO_START_TIME; 1125 } 1126 return start; 1127 } 1128 1129 /* The logic is similar to bpf_prog_run(), but with an explicit 1130 * rcu_read_lock() and migrate_disable() which are required 1131 * for the trampoline. The macro is split into 1132 * call __bpf_prog_enter 1133 * call prog->bpf_func 1134 * call __bpf_prog_exit 1135 * 1136 * __bpf_prog_enter returns: 1137 * 0 - skip execution of the bpf prog 1138 * 1 - execute bpf prog 1139 * [2..MAX_U64] - execute bpf prog and record execution time. 1140 * This is start time. 1141 */ 1142 static u64 notrace __bpf_prog_enter_recur(struct bpf_prog *prog, struct bpf_tramp_run_ctx *run_ctx) 1143 __acquires(RCU) 1144 { 1145 rcu_read_lock_dont_migrate(); 1146 1147 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1148 1149 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 1150 bpf_prog_inc_misses_counter(prog); 1151 if (prog->aux->recursion_detected) 1152 prog->aux->recursion_detected(prog); 1153 return 0; 1154 } 1155 return bpf_prog_start_time(); 1156 } 1157 1158 static void notrace __update_prog_stats(struct bpf_prog *prog, u64 start) 1159 { 1160 struct bpf_prog_stats *stats; 1161 unsigned long flags; 1162 u64 duration; 1163 1164 /* 1165 * static_key could be enabled in __bpf_prog_enter* and disabled in 1166 * __bpf_prog_exit*. And vice versa. Check that 'start' is valid. 1167 */ 1168 if (start <= NO_START_TIME) 1169 return; 1170 1171 duration = sched_clock() - start; 1172 stats = this_cpu_ptr(prog->stats); 1173 flags = u64_stats_update_begin_irqsave(&stats->syncp); 1174 u64_stats_inc(&stats->cnt); 1175 u64_stats_add(&stats->nsecs, duration); 1176 u64_stats_update_end_irqrestore(&stats->syncp, flags); 1177 } 1178 1179 static __always_inline void notrace update_prog_stats(struct bpf_prog *prog, 1180 u64 start) 1181 { 1182 if (static_branch_unlikely(&bpf_stats_enabled_key)) 1183 __update_prog_stats(prog, start); 1184 } 1185 1186 static void notrace __bpf_prog_exit_recur(struct bpf_prog *prog, u64 start, 1187 struct bpf_tramp_run_ctx *run_ctx) 1188 __releases(RCU) 1189 { 1190 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1191 1192 update_prog_stats(prog, start); 1193 bpf_prog_put_recursion_context(prog); 1194 rcu_read_unlock_migrate(); 1195 } 1196 1197 static u64 notrace __bpf_prog_enter_lsm_cgroup(struct bpf_prog *prog, 1198 struct bpf_tramp_run_ctx *run_ctx) 1199 __acquires(RCU) 1200 { 1201 /* Runtime stats are exported via actual BPF_LSM_CGROUP 1202 * programs, not the shims. 1203 */ 1204 rcu_read_lock_dont_migrate(); 1205 1206 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1207 1208 return NO_START_TIME; 1209 } 1210 1211 static void notrace __bpf_prog_exit_lsm_cgroup(struct bpf_prog *prog, u64 start, 1212 struct bpf_tramp_run_ctx *run_ctx) 1213 __releases(RCU) 1214 { 1215 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1216 1217 rcu_read_unlock_migrate(); 1218 } 1219 1220 u64 notrace __bpf_prog_enter_sleepable_recur(struct bpf_prog *prog, 1221 struct bpf_tramp_run_ctx *run_ctx) 1222 { 1223 rcu_read_lock_trace(); 1224 migrate_disable(); 1225 might_fault(); 1226 1227 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1228 1229 if (unlikely(!bpf_prog_get_recursion_context(prog))) { 1230 bpf_prog_inc_misses_counter(prog); 1231 if (prog->aux->recursion_detected) 1232 prog->aux->recursion_detected(prog); 1233 return 0; 1234 } 1235 return bpf_prog_start_time(); 1236 } 1237 1238 void notrace __bpf_prog_exit_sleepable_recur(struct bpf_prog *prog, u64 start, 1239 struct bpf_tramp_run_ctx *run_ctx) 1240 { 1241 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1242 1243 update_prog_stats(prog, start); 1244 bpf_prog_put_recursion_context(prog); 1245 migrate_enable(); 1246 rcu_read_unlock_trace(); 1247 } 1248 1249 static u64 notrace __bpf_prog_enter_sleepable(struct bpf_prog *prog, 1250 struct bpf_tramp_run_ctx *run_ctx) 1251 { 1252 rcu_read_lock_trace(); 1253 migrate_disable(); 1254 might_fault(); 1255 1256 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1257 1258 return bpf_prog_start_time(); 1259 } 1260 1261 static void notrace __bpf_prog_exit_sleepable(struct bpf_prog *prog, u64 start, 1262 struct bpf_tramp_run_ctx *run_ctx) 1263 { 1264 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1265 1266 update_prog_stats(prog, start); 1267 migrate_enable(); 1268 rcu_read_unlock_trace(); 1269 } 1270 1271 static u64 notrace __bpf_prog_enter(struct bpf_prog *prog, 1272 struct bpf_tramp_run_ctx *run_ctx) 1273 __acquires(RCU) 1274 { 1275 rcu_read_lock_dont_migrate(); 1276 1277 run_ctx->saved_run_ctx = bpf_set_run_ctx(&run_ctx->run_ctx); 1278 1279 return bpf_prog_start_time(); 1280 } 1281 1282 static void notrace __bpf_prog_exit(struct bpf_prog *prog, u64 start, 1283 struct bpf_tramp_run_ctx *run_ctx) 1284 __releases(RCU) 1285 { 1286 bpf_reset_run_ctx(run_ctx->saved_run_ctx); 1287 1288 update_prog_stats(prog, start); 1289 rcu_read_unlock_migrate(); 1290 } 1291 1292 void notrace __bpf_tramp_enter(struct bpf_tramp_image *tr) 1293 { 1294 percpu_ref_get(&tr->pcref); 1295 } 1296 1297 void notrace __bpf_tramp_exit(struct bpf_tramp_image *tr) 1298 { 1299 percpu_ref_put(&tr->pcref); 1300 } 1301 1302 bpf_trampoline_enter_t bpf_trampoline_enter(const struct bpf_prog *prog) 1303 { 1304 bool sleepable = prog->sleepable; 1305 1306 if (bpf_prog_check_recur(prog)) 1307 return sleepable ? __bpf_prog_enter_sleepable_recur : 1308 __bpf_prog_enter_recur; 1309 1310 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1311 prog->expected_attach_type == BPF_LSM_CGROUP) 1312 return __bpf_prog_enter_lsm_cgroup; 1313 1314 return sleepable ? __bpf_prog_enter_sleepable : __bpf_prog_enter; 1315 } 1316 1317 bpf_trampoline_exit_t bpf_trampoline_exit(const struct bpf_prog *prog) 1318 { 1319 bool sleepable = prog->sleepable; 1320 1321 if (bpf_prog_check_recur(prog)) 1322 return sleepable ? __bpf_prog_exit_sleepable_recur : 1323 __bpf_prog_exit_recur; 1324 1325 if (resolve_prog_type(prog) == BPF_PROG_TYPE_LSM && 1326 prog->expected_attach_type == BPF_LSM_CGROUP) 1327 return __bpf_prog_exit_lsm_cgroup; 1328 1329 return sleepable ? __bpf_prog_exit_sleepable : __bpf_prog_exit; 1330 } 1331 1332 int __weak 1333 arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *image_end, 1334 const struct btf_func_model *m, u32 flags, 1335 struct bpf_tramp_links *tlinks, 1336 void *func_addr) 1337 { 1338 return -ENOTSUPP; 1339 } 1340 1341 void * __weak arch_alloc_bpf_trampoline(unsigned int size) 1342 { 1343 void *image; 1344 1345 if (WARN_ON_ONCE(size > PAGE_SIZE)) 1346 return NULL; 1347 image = bpf_jit_alloc_exec(PAGE_SIZE); 1348 if (image) 1349 set_vm_flush_reset_perms(image); 1350 return image; 1351 } 1352 1353 void __weak arch_free_bpf_trampoline(void *image, unsigned int size) 1354 { 1355 WARN_ON_ONCE(size > PAGE_SIZE); 1356 /* bpf_jit_free_exec doesn't need "size", but 1357 * bpf_prog_pack_free() needs it. 1358 */ 1359 bpf_jit_free_exec(image); 1360 } 1361 1362 int __weak arch_protect_bpf_trampoline(void *image, unsigned int size) 1363 { 1364 WARN_ON_ONCE(size > PAGE_SIZE); 1365 return set_memory_rox((long)image, 1); 1366 } 1367 1368 int __weak arch_bpf_trampoline_size(const struct btf_func_model *m, u32 flags, 1369 struct bpf_tramp_links *tlinks, void *func_addr) 1370 { 1371 return -ENOTSUPP; 1372 } 1373 1374 static int __init init_trampolines(void) 1375 { 1376 int i; 1377 1378 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1379 INIT_HLIST_HEAD(&trampoline_key_table[i]); 1380 for (i = 0; i < TRAMPOLINE_TABLE_SIZE; i++) 1381 INIT_HLIST_HEAD(&trampoline_ip_table[i]); 1382 return 0; 1383 } 1384 late_initcall(init_trampolines); 1385